关闭计算机
function ShutDownSystem():BOOL;
var
hProcess,hAccessToken:THandle;
LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
TOKEN_PRIVILEGES: TTokenPrivileges;
BufferIsNull
WORD;
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;
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;
重启计算机:
{$StackFrames On} //QT_Thunk needs a stack frame, Thunking call to 16-bit USER.EXE. The ThunkTrash argument allocates space on the stack for QT_Thunk
function LoadLibrary16(LibraryName: PChar): THandle;
stdcall;
external kernel32 index 35;
procedure FreeLibrary16(HInstance: THandle);
stdcall;
external kernel32 index 36;
function GetProcAddress16(Hinstance: THandle;
ProcName: PChar): Pointer;
stdcall;
external kernel32 index 37;
procedure QT_Thunk;
cdecl;
external kernel32 name 'QT_Thunk';
var
hInst16: THandle;
GFSR: Pointer;
function RestartWindows: WordBool;
var
ThunkTrash: array[0..$20] of Word;
dw: DWord;
w: Word;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Result := False;
Exit;
end;
ThunkTrash[0] := hInst16;
//Prevent the optimizer from getting rid of ThunkTrash
hInst16 := LoadLibrary16('user.exe');
if hInst16 < 32 then
raise Exception.Create('Cannot load USER.EXE');
FreeLibrary16(hInst16);
//Decrement the usage count. Thisdo
esn't really free the library, since USER.EXE is always loaded
GFSR := GetProcAddress16(hInst16, 'ExitWindows');
//Get the function pointer for the 16-bit function in USER.EXE
if GFSR = nil then
raise Exception.Create('Cannot get address of ExitWindows');
dw := EW_RestartWindows;
w := 0;
asm //Thunkdo
wn to USER.EXE
push dw { push arguments }
push w
mov edx, GFSR { load 16-bit procedure pointer }
call QT_Thunk { call thunk }
mov Result, ax { save the result }
end;
end;
{$StackFrames Off}