我收集的四个重新启动/关闭WinNT/2000的代码,没有实验过,灌水喽[
][
][
]<br>{-------------------------------------------------------------------------}<br>procedure ReBootWinNT;<br>var<br> currToken:THandle;<br> prevState,newState:TTokenPrivileges;<br> prevStateLen
WORD;<br> uid:TLargeInteger;<br>begin<br> // Set the privledges so we CAN reboot...<br> OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, currToken);<br> LookupPrivilegeValue(nil, 'SeShutdownPrivilege',uid);<br> newState.PrivilegeCount:=1;<br> newState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br> newState.Privileges[0].Luid := uid;<br> AdjustTokenPrivileges(currToken, False, newState, sizeof(TTokenPrivileges),prevState, prevStateLen);<br> // Go ahead and reboot...<br> ExitWindowsEX(EWX_REBOOT, 0);<br>end;<br>{-------------------------------------------------------------------------}<br><br>function ShutDown(uFlags: Cardinal):boolean;<br>const<br> ADJUST_PRIV = TOKEN_QUERY or TOKEN_ADJUST_PRIVILEGES;<br> SHTDWN_PRIV = 'SeShutdownPrivilege';<br> PRIV_SIZE = sizeOf(TTokenPrivileges);<br>var<br> Len: DWORD;<br> TokenPriv, Dummy: TTokenPrivileges;<br> Token: THandle;<br> Error:integer;<br>begin<br> error:=0;<br> // 设置特权<br> if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, Token) then<br> Error := Error or 4;<br> if not LookupPrivilegeValue(nil, SHTDWN_PRIV,TokenPriv.Privileges[0].Luid) then<br> Error := Error or 8;<br> TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br> TokenPriv.PrivilegeCount := 1; // One privilege to set<br> if not AdjustTokenPrivileges(Token, false, TokenPriv, PRIV_SIZE,Dummy, Len) then<br> Error:=Error or 16;<br> ExitWindowsEx(uFlags, 0);<br> Result := (Error=0);<br>end;<br><br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br> ShutDown(EWX_POWEROFF + EWX_FORCE);<br>end;<br>{--------------------------------------------------------------------------}<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> AdjustToken;<br> ExitWindowsEx(EWX_SHUTDOWN, 0);<br>end;<br><br>//获取NT系统的操作权限<br>procedure AdjustToken();<br>var<br> hdlProcessHandle : Cardinal;<br> hdlTokenHandle : Cardinal;<br> tmpLuid : Int64;<br> tkp : TOKEN_PRIVILEGES;<br> tkpNewButIgnored : TOKEN_PRIVILEGES;<br> lBufferNeeded : Cardinal;<br> Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br> hdlProcessHandle := GetCurrentProcess;<br> OpenProcessToken(hdlProcessHandle,<br> (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br> hdlTokenHandle);<br> // Get the LUID for shutdown privilege.<br> LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> Privilege[0].Luid := tmpLuid;<br> Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> tkp.PrivilegeCount := 1; // One privilege to set<br> tkp.Privileges[0] := Privilege[0];<br> // Enable the shutdown privilege in the access token of this process.<br> AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br> tkpNewButIgnored,lBufferNeeded);<br>end;<br>{---------------------------------------------------------------------------}<br> function GetWinVersion: String;<br> var<br> VersionInfo : TOSVersionInfo;<br> OSName : String;<br> begin<br> // set the size of the record<br> VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );<br> if Windows.GetVersionEx( VersionInfo ) then<br> begin<br> with VersionInfo do<br> begin<br> case dwPlatformId of<br> VER_PLATFORM_WIN32s : OSName := 'Win32s';<br> VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';<br> VER_PLATFORM_WIN32_NT : OSName := 'Windows NT';<br> end; // case dwPlatformId<br> Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) + #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';<br> end; // with VersionInfo<br> end // if GetVersionEx<br> else<br> Result := '';<br> end;<br><br> procedure ShutDown;<br> const<br> SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration<br> var<br> hToken : THandle;<br> tkp : TTokenPrivileges;<br> tkpo : TTokenPrivileges;<br> zero : DWORD;<br> begin<br> if Pos( 'Windows NT', GetWinVersion) = 1 then // we've got to do a whole buch of things<br> begin<br> zero := 0;<br> if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br> begin<br> MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br> Exit;<br> end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br> if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br><br> begin<br><br> MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br><br> Exit;<br><br> end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br><br><br> // SE_SHUTDOWN_NAME<br><br> if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then<br><br> begin<br><br> MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );<br><br> Exit;<br><br> end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )<br><br> tkp.PrivilegeCount := 1;<br><br> tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br><br><br> AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br><br> if Boolean( GetLastError() ) then<br><br> begin<br><br> MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );<br><br> Exit;<br><br> end // if Boolean( GetLastError() )<br><br> else<br><br> ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br><br> end // if OSVersion = 'Windows NT'<br><br> else<br><br> begin // just shut the machine down<br><br> ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br><br> end; // else<br><br> end;<br><br>这个程序适用于所有Windows版本 <br><br><br><br>另给出调整关机权限:<br>procedure TForm1.AdjustToken();<br>var<br> hdlProcessHandle : Cardinal;<br> hdlTokenHandle : Cardinal;<br> tmpLuid : Int64;<br> tkpPrivilegeCount : Int64;<br> tkp : TOKEN_PRIVILEGES;<br> tkpNewButIgnored : TOKEN_PRIVILEGES;<br> lBufferNeeded : Cardinal;<br> Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br> hdlProcessHandle := GetCurrentProcess;<br> OpenProcessToken(hdlProcessHandle,<br> (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br> hdlTokenHandle);<br><br> // Get the LUID for shutdown privilege.<br> LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> Privilege[0].Luid := tmpLuid;<br> Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> tkp.PrivilegeCount := 1; // One privilege to set<br> tkp.Privileges[0] := Privilege[0];<br> // Enable the shutdown privilege in the access token of this<br> // process.<br> AdjustTokenPrivileges(hdlTokenHandle,<br> False,<br> tkp,<br> Sizeof(tkpNewButIgnored),<br> tkpNewButIgnored,<br> lBufferNeeded);<br><br>end;<br>