procedure GetTokenPrivilege(const RequiredPriv: String);
var
TokenHandle: THandle;
Privs: TTokenPrivileges;
OldSize: Cardinal;
Luid: Int64;
begin
if not LookupPrivilegeValue(nil, PChar(RequiredPriv), Luid) then
begin
Debug(SysErrorMessage(GetLastError));
Exit;
end;
Privs.PrivilegeCount := 1;
Privs.Privileges[0].Luid := Luid;
Privs.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, TokenHandle) then
begin
if not AdjustTokenPrivileges(TokenHandle, False, Privs, 0, nil, OldSize) then
Debug(SysErrorMessage(GetLastError));
end
else
Debug(SysErrorMessage(GetLastError));
end;
procedure Shutdown(Reboot: Boolean);
const
ShutDownFlags: array[Boolean] of Cardinal = (EWX_SHUTDOWN or EWX_POWEROFF, EWX_REBOOT);
begin
GetTokenPrivilege('seShutdownPrivilege');
if not ExitWindowsEx(ShutDownFlags[Reboot] + EWX_FORCE, 0) then
raise Exception.Create(SysErrorMessage(GetLastError));
end;