给你一个参考
procedure TKStatus.ShutDown;
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
var
hToken:THandle;
tkp:TTokenPrivileges;
tkpo:TTokenPrivileges;
zero:DWORD;
begin
if Pos('Windows NT',GetWinVersion) = 1 then
begin
zero := 0;
if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken) then
begin
Exit;
end;
if not LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid) then
begin
Exit;
end;
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,False,tkp,SizeOf(TTokenPrivileges),tkpo,zero);
if Boolean(GetLastError()) then
begin
Exit;
end
else
ExitWindowsEx(EWX_FORCE or EWX_POWEROFF,0); //ExitWindowsEx(EWX_SHUTDOWN,0); //
end
else
begin
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN,0); //ExitWindowsEx(EWX_SHUTDOWN,0); //
end;
end;
function TFrmMain.ShutDownSystem(si:integer):BOOL;
var
hProcess,hAccessToken:THandle;
LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
TOKEN_PRIVILEGES: TTokenPrivileges;
BufferIsNull:DWORD;
Const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
begin
hProcess:=GetCurrentProcess();
AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull);
case si of
1:
ExitWindowsEx(EWX_REBOOT, 0); //重启
2:
ExitWindowsEx(EWX_POWEROFF, 0); //关机
end;
ShutDownSystem:=True;
end;