Exiting windows (32-bit)
   


Another commonly asked question: How do I exit Windows? This procedure will do the trick for you and it works on all 32-bit Windows platforms.

Note 1: This procedure uses GetWindowsVersion() to read the Windows version info.
Note 2: Be careful when trying this in the Delphi IDE, it WILL restart or shutdown the computer! Save your work!

procedure ExitWindows32(ShutDownFlag: Word);
{ proc to Exit 32-bit Windows. ShutDownFlag is either EWX_REBOOT, EWX_SHUTDOWN, or EWX_LOGOFF. }

  function ChangeNTSecurityForShutdown: Boolean;
  { This func changes security rights on a WinNT machine. }
  { to give app shutdown privileges. }
  { Use proc ExitWindows32 to reboot or shutdown the machine. }
  var
    hToken : THandle;
    tkp,
    Newt : TTokenPrivileges;
    retlength : DWORD;
  begin
    Result := False;
    if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken) <> False then
      begin
        { Get the LUID for shutdown privilege }
        if LookupPrivilegeValue( nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) = True then
          begin
            tkp.PrivilegeCount := 1; // One to set
            tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
            { Get shutdown privilege for this process }
            Result := AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), Newt, retlength);
          end;
      end;
  end;

begin
  if GetWindowsVersion = VER_PLATFORM_WIN32_NT then
    begin
      if ChangeNTSecurityForShutdown then
        ExitWindowsEx(ShutDownFlag, 0)
      else
        { Failed to change security rights to give us shutdown privilege. }
        MessageDlg('Unable to modify security rights for shutdown privileges.', mtError, [mbOK], 0);
    end
  else
    if GetWindowsVersion = VER_PLATFORM_WIN32_WINDOWS then
      ExitWindowsEx(ShutDownFlag, 0);
end;


Blue Orb Software

[email protected]