×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How To

How do I manipulate the computer (shutdown, restart, etc)? by Glenn9999
Posted: 6 Feb 08 (Edited 14 Feb 08)

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 Torry's Delphi Pages.  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:

Quote:


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:

Quote:


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;

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close