yoy.be "Why-o-Why"

ExitWindowsEx and setting the privilige for it, yourself!

2005-06-08 22:11  i94  delphi  [permalink]

ExitWindowsEx throws a privilege not set error by default, but there's a way to set this required privilege:

procedure RebootComputer;
var
 th:THandle;
 lid:Int64;
 tp,tpx:TTokenPrivileges;
 tpc:cardinal;
begin
 if not(OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,th)) then
  RaiseLastOSError;
 if not(LookupPrivilegeValue('','SeShutdownPrivilege',lid)) then
  RaiseLastOSError;
 tp.PrivilegeCount:=1;
  tp.Privileges[0].Luid:=lid;
  tp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
 
 tpc:=0;
 if not(AdjustTokenPrivileges(th,false,tp,SizeOf(tp),tpx,tpc)) then
  RaiseLastOSError;
 
 if not(ExitWindowsEx(EWX_REBOOT,0)) then RaiseLastOSError;
end;

twitter reddit linkedin facebook