Board index » delphi » Restarting Win95 from Delphi

Restarting Win95 from Delphi

Does anyone know how I could force a reboot via delphi???
 

Re:Restarting Win95 from Delphi


Include the Windows unit and put...

ExitWindowsEx(EWX_REBOOT, 0);

Where you want to restart the system.  I don't know whether this will force
a restart or simply restart windows.  There is probably a different flag for
simply restarting windows (like for example EWX_LOGOFF)  Use <EWX_REBOOT or
EWX_FORCE> to tell windows to force a restart (without notifying open
applications) in extreme circumstances _ONLY_

Kelly Leahy
schut...@yahoo.com

Quote
>Does anyone know how I could force a reboot via delphi???

Re:Restarting Win95 from Delphi


Quote
JIM VARRATI wrote in message ...
>Does anyone know how I could force a reboot via delphi???

Yep.

Oh you wanted to know how also. Here you go:

ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

        -- Kirk

Re:Restarting Win95 from Delphi


In <6qqn2u$...@bgtnsc03.worldnet.att.net> "kroma" <kr...@worldnet.att.net> writes:

Quote
>JIM VARRATI wrote in message ...
>>Does anyone know how I could force a reboot via delphi???
>Yep.
>Oh you wanted to know how also. Here you go:
>ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

How about WinNT?
EWX_SHUTDOWN does not do it.
--
Colin McFadyen - Systems Support - col...@ccs.carleton.ca
Carleton University Computing and Communications Services
Ottawa, Ontario, CANADA K1S 5B6                  

Re:Restarting Win95 from Delphi


  Yes it does, you just have to adjust the Token privileges...

--
Jason Wallace
SL Software
Dark...@SLSoftware.reno.nv.us
--
"We are Microsoft.  You will be assimilated.  Resistance is Futile."
--

Quote
Colin McFadyen wrote in message ...
>In <6qqn2u$...@bgtnsc03.worldnet.att.net> "kroma" <kr...@worldnet.att.net>
writes:

>>JIM VARRATI wrote in message ...
>>>Does anyone know how I could force a reboot via delphi???

>>Yep.

>>Oh you wanted to know how also. Here you go:

>>ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

>How about WinNT?
>EWX_SHUTDOWN does not do it.
>--
>Colin McFadyen - Systems Support - col...@ccs.carleton.ca
>Carleton University Computing and Communications Services
>Ottawa, Ontario, CANADA K1S 5B6

Re:Restarting Win95 from Delphi


On NT you have to make sure you have the right privileges. Here is some
source code from a freeware component I found recently. This works on NT
since I use it to reboot an NT sever remotely.

Sam :^)

unit WinReboot;

(* TWinReboot component (freeware)
   by Barry Brannan, September 1997
   Usage:
   1. Drop TWinReboot component on a form.
   2. Call WinReboot1.WinExit(flags)
   Where flags must be one of the following:
   EWX_LOGOFF     - Shuts down processes and logs user off
   EWX_REBOOT     - Shuts down the restarts the system
   EWX_SHUTDOWN   - Shuts down system
   The following attributes may be combined (OR'd) with above flags
   EWX_POWEROFF  - shuts down system and turns off the power.
   EWX_FORCE     - forces processes to terminate.

   Example:
           WinReboot1.WinExit(EWX_REBOOT or EWX_FORCE);
*)

interface

uses
  Windows, Classes;

type
  TWinReboot = class(TComponent)
  private
    function SetPrivilege(privilegeName: string; enable: boolean): boolean;
  public
    function WinExit(flags: integer): boolean;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TWinReboot]);
end;

function TWinReboot.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;

function TWinReboot.WinExit(flags: integer): boolean;
begin
  Result := True;
  SetPrivilege('SeShutdownPrivilege', true);
  if not ExitWindowsEx(flags, 0) then
      Result := False;
  SetPrivilege('SeShutdownPrivilege', False);
end;

end.

Quote
Colin McFadyen wrote in message ...
>In <6qqn2u$...@bgtnsc03.worldnet.att.net> "kroma" <kr...@worldnet.att.net>
writes:

>>JIM VARRATI wrote in message ...
>>>Does anyone know how I could force a reboot via delphi???

>>Yep.

>>Oh you wanted to know how also. Here you go:

>>ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

>How about WinNT?
>EWX_SHUTDOWN does not do it.
>--
>Colin McFadyen - Systems Support - col...@ccs.carleton.ca
>Carleton University Computing and Communications Services
>Ottawa, Ontario, CANADA K1S 5B6

Re:Restarting Win95 from Delphi


Quote
Colin McFadyen wrote in message ...
>In <6qqn2u$...@bgtnsc03.worldnet.att.net> "kroma" <kr...@worldnet.att.net>

writes:

Quote
>How about WinNT?
>EWX_SHUTDOWN does not do it.

Like someone else said you need the right privileges to shutdown in NT. Here
is a shorter version of what they posted and the version I always use.

uses
  Windows;

{$R *.RES}
var
hToken : THandle;
tkp, ptkp : TTokenPrivileges;
i : dword;

begin
//* Get a token for this process. */

OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
hToken);

//* Get the LUID for the shutdown privilege. */

LookupPrivilegeValue(NiL, 'SE_SHUTDOWN_NAME', tkp.Privileges[0].Luid);

tkp.PrivilegeCount := 1;  ///* one privilege to set    */
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

//* Get the shutdown privilege for this process. */
AdjustTokenPrivileges(hToken,FALSE,tkp,0,ptkp,i);

//* Cannot test the return value of AdjustTokenPrivileges. */

GetLastError;

//* Shut down the system and force all applications to close. */

ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

end.

Other Threads