重起计算机: procedure Texitform.reboot_computer; var hToken: THandle; tkp: TOKEN_PRIVILEGES; ReturnLength: DWord; begin if (not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken)) then begin application.Terminate; end; LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid); tkp.PrivilegeCount := 1; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; ReturnLength := 0; AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, ReturnLength); if (GetLastError() <> ERROR_SUCCESS) then begin application.Terminate; end; if (not ExitWindowsEx(EWX_REBOOT, 0)) then begin application.Terminate; end; end;
procedure TForm1.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; Privil ege[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);end; ****************** 在Windows2000下关闭计算机 function ShutDownSystem():BOOL; var hProcess,hAccessToken:THandle; LUID_AND_ATTRIBUTES:TLUIDAndAttributes; TOKEN_PRIVILEGES: TTokenPrivileges; BufferIsNull:DWORD; Const SE_SHUTDOWN_NAME='SeShutdownPrivilege'; begin hProcess:=GetCurrentProcess();OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken); LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid); LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED; TOKEN_PRIVILEGES.PrivilegeCount:=1; TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES; BufferIsNull:=0;AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVI LEGES),Nil,BufferIsNull); ExitWindowsEx(EWX_REBOOT, 0); ShutDownSystem:=True; end; //打开光驱 mciSendString('Set cdaudio door open wait', nil, 0, handle); //关闭光驱 mciSendString('Set cdaudio door closed wait', nil, 0, handle)
procedure Texitform.reboot_computer;
var
hToken: THandle;
tkp: TOKEN_PRIVILEGES;
ReturnLength: DWord;
begin if (not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or
TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken)) then
begin
application.Terminate;
end;
LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
ReturnLength := 0;
AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, ReturnLength);
if (GetLastError() <> ERROR_SUCCESS) then
begin
application.Terminate;
end; if (not ExitWindowsEx(EWX_REBOOT, 0)) then
begin
application.Terminate;
end;
end;
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;
Privil
ege[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);end;
******************
在Windows2000下关闭计算机
function ShutDownSystem():BOOL;
var
hProcess,hAccessToken:THandle;
LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
TOKEN_PRIVILEGES: TTokenPrivileges;
BufferIsNull:DWORD;
Const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
begin
hProcess:=GetCurrentProcess();OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid);
LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED;
TOKEN_PRIVILEGES.PrivilegeCount:=1;
TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES;
BufferIsNull:=0;AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVI
LEGES),Nil,BufferIsNull);
ExitWindowsEx(EWX_REBOOT, 0);
ShutDownSystem:=True;
end;
//打开光驱
mciSendString('Set cdaudio door open wait', nil, 0, handle);
//关闭光驱
mciSendString('Set cdaudio door closed wait', nil, 0, handle)