begin
error:=0;
begin
// Get a token for this process.
// Delphi2:
//if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, @Token) then
if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, Token) then
Error:=Error or 4;
{ ShowMessage('OpenProcessToken failed');}
// Get the LUID for the shutdown privilege.
if not LookupPrivilegeValue(nil, SHTDWN_PRIV,TokenPriv.Privileges[0].Luid) then
Error:=Error or 8;
{ShowMessage('LookupPrivilegeValue failed');}
// Adjust shutdown privilege for this process.
TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
TokenPriv.PrivilegeCount := 1; // One privilege to set
if not AdjustTokenPrivileges(Token, false, TokenPriv, PRIV_SIZE,Dummy, Len) then
Error:=Error or 16;
{ShowMessage('AdjustTokenPrivileges failed');}
{ if (shoForce in FOptions) then Force:=true
else Force:=false;
if (shoReboot in FOptions)then Reboot:=true
else Reboot:=false;}
InitiateSystemShutDown (Machine,Text,5,True,True);
end;
用了下面的函数,就可以用ExitWindow......关机了。这个问题回答过的,下面的代码是以前的贴子中Copy来的。
procedure AdjustToken();
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkpPrivilegeCount : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
hdlTokenHandle);
// Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this
// process.
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded);