需要设置权限
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;
end;
调用:
SetPrivilege(''SeShutdownPrivilege'',true);
ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE or EWX_POWEROFF,0);
当然你也可以用
ShellExecute(Handle,''open'',''shutdown.exe'', '' -s -t 0'',nil,SW_HIDE);
来实现关机的操作