Home | Articles | Links | Contact
 
Title How to log off, shutdown, reboot and power off the system from Delphi in Windows 95/98 and Windows NT
Description Do you need to power off the system from your application? Of course, you wouldn't be reading this article if you didn't.
Date 10/26/2000
Last revision
Related articles

Get the user name of the Windows current session


The function ExitWindowsEx initiates the process of closing the current session and, if needed, shutting down the system. This function gets two parameters, the first one defines the actions we want to perform as a list of flags we can activate. The second one is not used. The available values for the flags are:

EWX_LOGOFF Close the current session
EWX_POWEROFF Power off the system
EWX_REBOOT Reboot and start windows again
EWX_SHUTDOWN Shut down the system
EWX_FORCE Force processes to terminate, even losing information in the process

Valid calls for this function would be:

ExitWindowsEx( EWX_LOGOFF, 0)

End the current session without shutting the system down
ExitWindowsEx( EWX_REBOOT, 0) Rebooting the system
ExitWindowsEx( EWX_SHUTDOWN + EWX_FORCE, 0) Shutting the system down. The system won't ask the applications if they are ready to be closed

If you want to perform a EWX_POWEROFF, EWX_REBOOT or EWX_SHUTDOWN in Windows NT, you will need to make your program get the proper privileges. We are not going to explain the meaning of all the steps requiered to get those privileges in this article, but here you have a piece of code to perform a nice shutdown on a NT system:

if not OpenProcessToken( GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken) then
  raise Exception.Create( 'Error when opening the process token');
LookupPrivilegeValue( nil,
                      'SeShutdownPrivilege',
                      tkp.Privileges[0].Luid);
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, False, tkp, 0, nil, l);
ExitWindowsEx( EWX_SHUTDOWN, 0)


Was this article useful for you?
Yes No
Any other comments will be welcome

Home | Articles | Links | Contact