Board index » delphi » Shutting down or Restarting WIN2000 using DELPHI 5

Shutting down or Restarting WIN2000 using DELPHI 5

Greetings to all ...

For a couple of years (under WIN95 and 98 SE) I have been using the
WIN API call ExitWindowsEX to restart and/or shutdown the OS through
my DELPHI application.

We just moved to Windows 2000 and am having a bit of trouble
understanding how to shutdown or restart WIN2000 inside of my
DELPHI application.

For example under WIN95 or 98 I used the following code:

To Restart:
begin
      ExitWindowsEx(EWX_REBOOT , 0);
end;
- OR -
To Shutdown:
begin
      ExitWindowsEx(EWX_SHUTDOWN , 0);
end;

I have tried to execute these commands under Window 2000 and they have
absolutely NO effect.   Does anyone have any idea how I can do this?
Does anyone have sample code I could use?

Thanks in advance for any assistance!

--
Phil Tusa

 

Re:Shutting down or Restarting WIN2000 using DELPHI 5


I had the same problem until I did some reading. It is all to do with
privileges!!!!!
THis should begin to point you in the right direction.

function ExitWindows: Boolean;
var
  TTokenHd: THandle;
  TTokenPvg: TTokenPrivileges;
  cbtpPrevious: DWORD;
  rTTokenPvg: TTokenPrivileges;
  pcbtpPreviousRequired: DWORD;
  tpResult: Boolean;
  RebootParam : LongWord;
const
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    tpResult := OpenProcessToken(GetCurrentProcess(),
      TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TTokenHd);
    if tpResult then
    begin
      tpResult := LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME,
        TTokenPvg.Privileges[0].Luid);
      TTokenPvg.PrivilegeCount := 1;
      TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      cbtpPrevious := SizeOf(rTTokenPvg);
      pcbtpPreviousRequired := 0;
      if tpResult then
         Windows.AdjustTokenPrivileges(TTokenHd, False,
          TTokenPvg, cbtpPrevious, rTTokenPvg, pcbtpPreviousRequired);
    end;
    RebootParam := EWX_PowerOff;
  end
  else
    RebootParam := EWX_Shutdown;

  Result := ExitWindowsEx(RebootParam, 0);
end;

Re:Shutting down or Restarting WIN2000 using DELPHI 5


Hi Brian ...  Thanks for your quick response!

Quote
>I had the same problem until I did some reading. It is all to do with
>privileges!!!!!
>THis should begin to point you in the right direction.

Other Threads