Member Login

Remember Me
Forgot Password?
Join Us!

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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

How To

How do I manipulate the computer (shutdown, restart, etc)?
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.

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:


procedure machine_shutdown;
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);

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.

This code restarts a computer:


procedure machine_restart;
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);

This code logs off the current user:


procedure machine_logoff;
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
    NTSetPrivilege(SE_SHUTDOWN_NAME, True);
    ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0);

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.


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

IsPwrShutdownAllowed is described later.

Lock the Workstation

To lock the workstation, requiring the password if set is:


function LockWorkStation: boolean; stdcall; external 'user32.dll' name 'LockWorkStation';

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

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:


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:


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

To put the system into hibernate, do this:


procedure machine_hibernate;
    if IsHibernateAllowed then
      SetSuspendState(true, false, false)
      MessageDlg('System Hibernate not supported on this system.', mtWarning, [mbOK], 0);  

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