1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
   | program ForceShutDown;
 
uses Windows;
 
{$R *.res}
 
procedure ExitWindowsMsg(const PlatformCaption: PChar);
begin
  case MessageBox(0, PChar('Windows '+PlatformCaption+' : ShutDown(Yes) or Reboot(No) ?'), 'Confirmation', MB_YESNOCANCEL) of
    IDYES : if System.DebugHook = 0 then ExitWindowsEx(EWX_SHUTDOWN, 0) else Beep(3000, 1);
    IDNO : if System.DebugHook = 0 then ExitWindowsEx(EWX_REBOOT, 0) else Beep(2000, 1);
    else
      if System.DebugHook = 0 then Exit else Beep(1000, 1);
  end;
end;
 
var
  OSInfo: OSVERSIONINFO;
  TokenHandle, ReturnLength: Cardinal;
  NewState, PreviousState: TTokenPrivileges;
  lpLuid: TLargeInteger;
begin
  OSInfo.dwOSVersionInfoSize := SizeOf(OSVERSIONINFO);
  if GetVersionEx(OSInfo) then
  begin
    if OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then
    begin
      if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
      begin
        try
          if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', lpLuid) then
          begin
            NewState.PrivilegeCount := 1;
            NewState.Privileges[0].Luid := lpLuid;
            NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
            if AdjustTokenPrivileges(TokenHandle, False, NewState, SizeOf(NewState), PreviousState, ReturnLength) then
            begin
              ExitWindowsMsg('NT');
            end;
          end;
        finally
          CloseHandle(TokenHandle);
        end;
      end;
    end else
    begin
      ExitWindowsMsg('9x');
    end;
  end;
end. | 
Partager