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