Windows Reboot, Shutdown, PowerOff(Tipp drucken)



Die Routine ist für Windows 95/98/Me sehr einfach. Für NT, 2000 und XP benötigen Sie jedoch Privilegien, um einen Systemneustart durchzuführen. Die erste Routine prüft dabei das Betriebssystem, die zweite "schießt" das System ab.

Betriebssystemversion erfragen
function GetSystemVersion: Integer; // 0 - Windows 95/98/Me, 1 - WinNT/2000/XP/2003
var
  os: _OSVERSIONINFOA;
begin
  os.dwOSVersionInfoSize := sizeof(os);
  GetVersionEx(os);
  case os.dwPlatformId of
    VER_PLATFORM_WIN32s       : Result := -1;
    VER_PLATFORM_WIN32_WINDOWS: Result := 0;
    VER_PLATFORM_WIN32_NT:      Result := 1;
    else
      Result := -1;
  end;
end;
Neustart des Rechners
procedure RebootWindows;
var
  Success: Boolean;
  TokenPriv: TTokenPrivileges;
  TokenHandle: THandle;
  CurrentProc: THandle;
  vRes: Integer;
begin
  Success := false;

  vRes := GetSystemVersion;
  if vRes = 1 then // Win NT
  begin
    CurrentProc := GetCurrentProcess;

    if OpenProcessToken(CurrentProc, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
    begin
      if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', TokenPriv.Privileges[0].LUID) then
      begin
        TokenPriv.PrivilegeCount := 1;
        TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

        if AdjustTokenPrivileges(TokenHandle, false, TokenPriv, 0, nil, nil) then
          // Change Options for HARD Shutdown, Reboot etc.
          // EWX_LOGOFF - Typipcally Logout of a user
          // EWX_POWEROFF - Shutting down the system and turns the power off - 
          //   needs system-power-off support (needs SE_SHUTDOWN_NAME - privilege)
          // EWX_REBOOT - typically reboot (needs SE_SHUTDOWN_NAME - privilege)
          // EWX_SHUTDOWN - typically shut down (needs SE_SHUTDOWN_NAME - privilege)
            // for power-off-feature you MUST use EWX_POWEROFF, even the system supports it
          // EWX_FORCE - Force shut down - BE CAREFULLY (not sending WM_QUERYENDSESSION or
          //   WM_ENDSESSION messages)
          // EWX_FORCEIFHUNG - Force shut down if process do not respond to WM_QUERYENDSESSION or
          //   WM_ENDSESSION

          Success := ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCEIFHUNG, 0);
        if not Success then
          ShowMessage('No Reboot possible');
      end;
    end
    else
      ShowMessage('No Reboot possible');
  end
  else
    if vRes = 0 then  // Windows 95/98 - dont need privileges
      ExitWindowsEx(EWX_SHUTDOWN, 0);
end;