以下的两个函数,执行WinExitInNT(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF)
function SetPrivilege(sPrivilegeName: string;
bEnabled: Boolean): Boolean;
var
TPPrev,TP:TTokenPrivileges;
Token:THandle;
dwRetLen
Word;
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
begin
TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
end;
end else
begin
TP.Privileges[0].Attributes:=0;
end;
dwRetLen:=0;
Result:=AdjustTokenPrivileges(
Token,
False,
TP,
SizeOf(TPPrev),
TPPrev,
dwRetLen
);
CloseHandle(Token);
end;
function WinExitInNT(iFlags: integer): boolean;
begin
Result:=True;
//SE_SHUTDONW_NAME的权限值是SEShutdownPrivilege
if (SetPrivilege('SeShutdownPrivilege',True)) then
begin
if (not ExitWindowsEx(iFlags,0)) then
Result:=False;
SetPrivilege('SeShutdownPrivilege',False);
end
else begin
Result:=False;//句柄错误
end;
end;