function SetPrivilege(sPrivilegeName: string;
bEnabled: 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 (sPrivilegeName), TP.Privileges[0].LUID) then
begin
if bEnabled 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;
function WinExit (iFlags: integer) : Boolean;
begin
result := true;
if SetPrivilege ('SeShutdownPrivilege', true) then
begin
if (not ExitWindowsEx (iFlags, 0)) then
begin
// handle errors...
result := False
end;
SetPrivilege ('SeShutdownPrivilege', False)
end
else
begin
// handle errors...
result := False
end;
end;
procedure FormCreate(Sender: TObject);
begin
WinExit(EWX_POWEROFF + EWX_FORCE);
end;