function SetPrivilege(sPrivilegeName:string;bEnabled:boolean):boolean;
var
TP,TPPre:TTokenPrivileges;
Token:THandle;
dwLength:DWORD;
begin
result := false;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
try
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;
dwLength := 0;
Result := AdjustTokenPrivileges(Token,false,TP,sizeof(TPPre),TPPre,dwLength);
end;
finally
CloseHandle(Token);
end;
var
TP,TPPre:TTokenPrivileges;
Token:THandle;
dwLength:DWORD;
begin
result := false;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
try
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;
dwLength := 0;
Result := AdjustTokenPrivileges(Token,false,TP,sizeof(TPPre),TPPre,dwLength);
end;
finally
CloseHandle(Token);
end;
end;
调用
SetPrivilege('SeShutdownPrivilege',true); ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE or EWX_POWEROFF,0);