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: 51: 52: 53: 54: 55: 56: 57: 58:
| uses Windows;
type TShutdownWindowsType = (swtShutdown, swtShutdownPowerOff, swtRestart, swtLogoff);
function ShutdownWindows (aType: TShutdownWindowsType): Boolean;
function ShutdownWindows (aType: TShutdownWindowsType): Boolean;
const cSE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; cFlagValue: Array [TShutdownWindowsType] Of UINT = ( EWX_SHUTDOWN, EWX_SHUTDOWN or EWX_POWEROFF, EWX_REBOOT, EWX_LOGOFF );
var OSVersionInfo: TOSVersionInfo; hToken: THandle; hProcess: THandle; TokenPriv: TTokenPrivileges; ReturnLength: DWORD;
begin Result := False;
OSVersionInfo.dwOSVersionInfoSize := SizeOf (OSVersionInfo); if not GetVersionEx (OSVersionInfo) then Exit;
if OSVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin hProcess := GetCurrentProcess; if not OpenProcessToken (hProcess, TOKEN_ADJUST_PRIVILEGES, hToken) then Exit;
if not LookupPrivilegeValue (nil, cSE_SHUTDOWN_NAME, TokenPriv.Privileges[0].Luid) then Exit;
TokenPriv.PrivilegeCount := 1; TokenPriv.Privileges [0].Attributes := SE_PRIVILEGE_ENABLED;
if not AdjustTokenPrivileges ( hToken, False, TokenPriv, 0, PTokenPrivileges (nil)^, ReturnLength ) then Exit;
CloseHandle (hToken); end;
ShutdownWindows := ExitWindowsEx (cFlagValue [aType], $FFFFFFFF); end |