function OpenThread(dwDesiredAccess: Longword; bInheritHandle: BOOL; dwThreadId: Longword): THandle; stdcall; external 'Kernel32.dll';
function ShutdownSystem(bForce: Boolean): Boolean;
var
Handle: THandle;
Priveleges: TOKEN_PRIVILEGES;
Size: Longword;
luid: int64;
begin
Result := False;
if not OpenThreadToken(OpenThread(PROCESS_ALL_ACCESS, False, GetCurrentThreadId()), TOKEN_ALL_ACCESS, True, Handle) and not OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, Handle) then
Exit;
if not LookupPrivilegeValue(nil, 'SeShutdownPrivilege', luid) then
Exit;
Priveleges.PrivilegeCount := 1;
Priveleges.Privileges[0].Luid := luid;
Priveleges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not AdjustTokenPrivileges(Handle, False, Priveleges, SizeOf(Priveleges), Priveleges, Size) then
Exit;
if bForce then
Result := ExitWindowsEx(EWX_FORCE or EWX_POWEROFF, 0) or ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)
else
Result := ExitWindowsEx(EWX_POWEROFF, 0) or ExitWindowsEx(EWX_SHUTDOWN, 0)
end;
---
|