MP_Reset: //--重启动
if Win32Platform <> VER_PLATFORM_WIN32_NT then //不是NT
ExitWindowsEx(EWX_REBOOT, 0)
else
begin
SetPrivilege('SeShutdownPrivilege', True);
if not ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 0) then
SetPrivilege('SeShutdownPrivilege', False);
end;
MP_Down: //--关机
if Win32Platform <> VER_PLATFORM_WIN32_NT then //不是NT
ShellExecute(handle, 'open', 'RUNDLL32.EXE', 'user.exe,ExitWindows', nil, SW_ShowNormal)
else
begin
SetPrivilege('SeShutdownPrivilege', True);
if not ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE+EWX_POWEROFF, 0) then
SetPrivilege('SeShutdownPrivilege', False);
end;
if (GetLastError() <> ERROR_SUCCESS) then
result := FALSE;
// Shut down the system and force all applications to close.
//EWX_POWEROFF,EWX_LOGOFF,EWX_SHUTDOWN
if not(ExitWindowsEx(EWX_REBOOT and EWX_FORCE, 0)) then
result := FALSE;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure AdjustToken;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
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;
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);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AdjustToken;
ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
end;
procedure PowerOff(Force:boolean=false);
var
hToken : THandle;
tkp : TTokenPrivileges;
tkpo : TTokenPrivileges;
zero : DWORD;
begin
if Win32Platform=VER_PLATFORM_WIN32_NT then // we've got to do a whole buch of things
begin
zero := 0;
if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
MessageBox( 0, 'PowerOff Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
// SE_SHUTDOWN_NAME
if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then
begin
MessageBox( 0, 'PowerOff Error', 'LookupPrivilegeValue() Failed', MB_OK );
Exit;
end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );
if Boolean( GetLastError() ) then
begin
MessageBox( 0, 'PowerOff Error', 'AdjustTokenPrivileges() Failed', MB_OK );
Exit;
end // if Boolean( GetLastError() )
else if Force then
ExitWindowsEx( EWX_POWEROFF, 0 )
else ExitWindowsEx( EWX_REBOOT, 0 );
end // if OSVersion = 'Windows NT'
else // just shut the machine down
if Force then ExitWindowsEx( EWX_POWEROFF, 0 )
else ExitWindowsEx( EWX_REBOOT, 0 );
end;