Board index » delphi » Shutting down or restarting DELPHI app under WIN 2000

Shutting down or restarting DELPHI app under WIN 2000

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 DELPHI app under WIN 2000


Quote
Phil Tusa wrote:

> 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

This is almost certainly a 'priveleges' issue.
From W32 help.......

Windows NT: To shut down or restart the system, the calling process must
use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
privilege. For more information about security privileges, see
Privileges.

Dave

Re:Shutting down or restarting DELPHI app under WIN 2000


Hello David  ....

Quote
>This is almost certainly a 'priveleges' issue.

Yes it is  :)

Quote
>From W32 help.......

I already read the on-line help this afternoon, and could not make any
sense of the explaination.  Would you have any sample code using the
AdjustTokenPrivileges function ???

Thanks for your quick response ...

--
Phil Tusa

Re:Shutting down or restarting DELPHI app under WIN 2000


Quote
Phil Tusa <pt...@houston.rr.com> wrote in message <news:ptud3vg318huo54tgetcvndbogo0hkh6kf@4ax.com>...
> 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!

Google should have found this, but for your convenience here it is again:

This will handle them all...need ShellAPI in uses clause:

function TformMain.Win9X: Boolean;
var
  versionInfo : TOSVersionInfo;
begin
  Result := False;
  // set the size of the record
  VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  Windows.GetVersionEx(VersionInfo);
  If (VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) then
    Result := True;
end;

function TformMain.WinExit(flags: integer): boolean;
  function SetPrivilege(privilegeName: string; enable: boolean): boolean;
  var tpPrev,
    tp : TTokenPrivileges;
    token : THandle;
    dwRetLen : DWord;
  begin
    result := False;
    OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
       TOKEN_QUERY, token);
    tp.PrivilegeCount := 1;
    if LookupPrivilegeValue(nil, pchar(privilegeName),
       tp.Privileges[0].LUID) then
    begin
      if enable then
        tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
      else
        tp.Privileges[0].Attributes := 0;
      dwRetLen := 0;
      result := AdjustTokenPrivileges(token, False, tp, SizeOf(tpPrev),
         tpPrev, dwRetLen);
    end;
    CloseHandle(token);
  end;
begin
  if SetPrivilege('SeShutdownPrivilege', true) then
  begin
    ExitWindowsEx(flags, 0);
    SetPrivilege('SeShutdownPrivilege', False)
  end;
  result := True;
end;

procedure TformMain.btnExitWindowsClick(Sender: TObject);
begin
  Close;
  If Win9X then ExitWindowsEx(EWX_SHUTDOWN, 0)
  else WinExit(EWX_POWEROFF);
end;

HTH,

John

Re:Shutting down or restarting DELPHI app under WIN 2000


In article <3d197c7d.0301282049.5257b...@posting.google.com>,

Quote
johnrkeb...@hotmail.com (John Kebert) writes:
>function TformMain.Win9X: Boolean;
>var
>  versionInfo : TOSVersionInfo;
>begin
>  Result := False;
>  // set the size of the record
>  VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
>  Windows.GetVersionEx(VersionInfo);
>  If (VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) then
>    Result := True;
>end;

VersionInfo.dwPlatformId already appars for you in Delphi.SysUtils as
Win32Platform. Similarly for other VersionInfo values.

Alan Lloyd
alangll...@aol.com

Re:Shutting down or restarting DELPHI app under WIN 2000


Quote
alangll...@aol.com (AlanGLLoyd) wrote in message <news:20030129023448.04776.00000257@mb-ct.aol.com>...

> VersionInfo.dwPlatformId already appears for you in Delphi.SysUtils as
> Win32Platform.  Similarly for other VersionInfo values.

> Alan Lloyd
> alangll...@aol.com

Thanks, I learn more about Delphi everyday by reading these newsqroups.

John

Other Threads