Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I manipulate the computer (shutdown, restart, etc)?

How To

How do I manipulate the computer (shutdown, restart, etc)?

by  Glenn9999  Posted    (Edited  )
It can be occasionally useful to do different things to the system within your program that can be done via the log off menu or otherwise. They include: Shutdown, Restart, Log off, Standby, Hibernate, and Power Off. These things will be described below:

System Privileges
Many of these things require the setting of SeShutdownPrivilege. For what I do, I ended up borrowing the code that you will find at [link http://www.swissdelphicenter.ch/torry/showcode.php?id=1177]Torry's Delphi Pages[/link]. Any code samples which require it will have the NTSetPrivilege function call that is found on that page.


Shutdown
All these functions involve variations of two system calls. This will start the first group. Shutting down the system involves a call to ExitWindowsEx. Delphi will have this procedure defined within the Windows unit, so all that is needed is to call the function. The first parm is an action, the second one is reserved and can always be 0.

To shutdown the system:
Code:
procedure machine_shutdown;
  const
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
  begin
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);
  end;

the first parm value is obvious, the second is not. In the Win32 help dll, the following is said about EWX_FORCE:
Forces processes to terminate. Instead of bringing up the "application not responding" dialog box for the user, this value forces an application to terminate if it does not respond.

Restart
This code restarts a computer:
Code:
procedure machine_restart;
  const
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
  begin
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
  end;

Logoff
This code logs off the current user:
Code:
procedure machine_logoff;
  const
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
  begin
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0);
  end;

Power Off
This code powers down the system. This is distinguished from the shut down command in that it will power off the system, while the shut down command will just put the computer to a point where it can be turned off.

Code:
  procedure machine_poweroff;
    const
      SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
    begin
      if IsPwrShutdownAllowed then
        begin
          NTSetPrivilege(SE_SHUTDOWN_NAME, True);
          ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);
        end
      else
        MessageDlg('Soft Power Off not supported on this system.', mtWarning, [mbOK], 0);
    end;

IsPwrShutdownAllowed is described later.

Lock the Workstation

To lock the workstation, requiring the password if set is:
Code:
function LockWorkStation: boolean; stdcall; external 'user32.dll' name 'LockWorkStation';

procedure machine_lock;
    begin
      if not LockWorkStation then
        MessageDlg('System not locked successfully.', mtWarning, [mbOK], 0);
    end;

Standby
This group starts the actions involving the other system function. I notice it is not defined in Delphi 3 (and perhaps not future Delphis), so I will show the definitions below for all the functions of interest:

Code:
function SetSuspendState(hibernate, forcecritical, disablewakeevent: boolean): boolean; stdcall; external 'powrprof.dll' name 'SetSuspendState';

function IsHibernateAllowed: boolean; stdcall; external 'powrprof.dll' name 'IsPwrHibernateAllowed';

function IsPwrSuspendAllowed: Boolean; stdcall; external 'powrprof.dll' name 'IsPwrSuspendAllowed';

function IsPwrShutdownAllowed: Boolean; stdcall; external 'powrprof.dll' name 'IsPwrShutdownAllowed';

The function names are quite obvious as to their functionality. To look at SetSuspendState, I will quote from the SDK regarding the parms:

Hibernate If this parameter is TRUE, the system hibernates. If the parameter is FALSE, the system is suspended.
ForceCritical If this parameter is TRUE, the system suspends operation immediately; if it is FALSE, the system broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation.
DisableWakeEvent If this parameter is TRUE, the system disables all wake events. If the parameter is FALSE, any system wake events remain enabled.

To put the system into standby mode, do this:

Code:
procedure machine_standby;
  begin
    if IsPwrSuspendAllowed then
      SetSuspendState(false, false, false)
    else
      MessageDlg('System Standby not supported on this system.', mtWarning, [mbOK], 0);
  end;

Hibernate
To put the system into hibernate, do this:

Code:
procedure machine_hibernate;
  begin
    if IsHibernateAllowed then
      SetSuspendState(true, false, false)
    else
      MessageDlg('System Hibernate not supported on this system.', mtWarning, [mbOK], 0);  
  end;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top