Mega Code Archive

 
Categories / Delphi / Examples
 

Shut down windows

How to force windows to shut down. Call the following routine like so WinExit(EWX_POWEROFF or EWX_FORCE); function WinExit(flags: integer): boolean; function SetPrivilege(privilegeName: string; enable: boolean): boolean; var tpPrev, tp : TTokenPrivileges; token : THandle; dwRetLen : DWord; begin result := False; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, token); tp.PrivilegeCount := 1; if LookupPrivilegeValue(nil, pchar(privilegeName), tp.Privileges[0].LUID) then begin if enable then tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else tp.Privileges[0].Attributes := 0; dwRetLen := 0; result := AdjustTokenPrivileges(token, False, tp, SizeOf(tpPrev), tpPrev, dwRetLen); end; CloseHandle(token); end; begin if SetPrivilege('SeShutdownPrivilege', true) then begin ExitWindowsEx(flags, 0); SetPrivilege('SeShutdownPrivilege', False) end; end;