[fpc-pascal]Windows ShutDown Privilege

MPDJ mpdj at btinternet.com
Fri Dec 6 07:46:47 CET 2002


Hi,

   I'm trying to shut down windows. I know it should be simple, but
for some reason, I can't get my application to have shutdown privileges.
The procedure I'm using is this...


Procedure Shut_Down_Windows;
Var
   hToken    : Handle;
   tkp       : TOKEN_PRIVILEGES;
   h         : PWORD;

begin
   If OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken) then Begin
      LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, tkp.Privileges[0].Luid);
      tkp.PrivilegeCount:=1;
      tkp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
      h:=0;
      AdjustTokenPrivileges(hToken, False, @tkp, SizeOf(tpk), nil , at h);
      ExitWindowsEx (EWX_SHUTDOWN, 0);
   End;
end.


OpenProcessToken returns TRUE, so does LookUpPriviliegeValue and
AdjustTokenPrivileges.

Problem is, ExitWindowsEX returns FALSE with the error that I don't
have the ShutDown privilege. I can't for the life of me spot the
problem. I'm sure it's going to be something stupid I've missed, but
I'm blind to it. It's failed on both my Win2000 and XP machines.

If anyone has a spare few seconds to see if they can spot my mistake,
I'd be very grateful.

Andy





More information about the fpc-pascal mailing list