退出系统<br> function GetWinVersion: String;<br><br> var<br><br> VersionInfo : TOSVersionInfo;<br><br> OSName : String;<br><br> begin<br><br> // set the size of the record<br><br> VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );<br><br><br> if Windows.GetVersionEx( VersionInfo ) then<br><br> begin<br><br> with VersionInfo do<br><br> begin<br><br> case dwPlatformId of<br><br> VER_PLATFORM_WIN32s : OSName := 'Win32s';<br><br> VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';<br><br> VER_PLATFORM_WIN32_NT : OSName := 'Windows NT';<br><br> end; // case dwPlatformId<br><br> Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +<br><br> #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';<br><br> end; // with VersionInfo<br><br> end // if GetVersionEx<br><br> else<br><br> Result := '';<br><br> end;<br><br><br> procedure ShutDown;<br><br> const<br><br> SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration<br><br> var<br><br> hToken : THandle;<br><br> tkp : TTokenPrivileges;<br><br> tkpo : TTokenPrivileges;<br><br> zero : DWORD;<br><br> begin<br><br> if Pos( 'Windows NT', GetWinVersion) = 1 then // we've got to do a whole buch of things<br><br> begin<br><br> zero := 0;<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> 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>