System Shutdown Windows API Functions
Windows API provides access to many functions that are not available as in-built functions with Visual Basic. System shutdown functions are just few of them. Let’s get them one by one.
LockWorkStation allows to lock the workstation. This function takes no parameter and returns zero if function fails otherwise non-zero. The function works on Windows XP or Windows 2000 Professional.
Declare Function LockWorkStation Lib "user32" () As Long LockWorkStation
ExitWindows logs off the current user. The two parameters that the function requires are reserved and should both be passed as Zeros. The function returns Zero if it fails otherwise it returns non-zero. The function when called, messages all open applications to enable saving unsaved files. If any of the open application refuses to close, the log-off process is stopped.
Declare Function ExitWindows Lib "user32" ( _
ByVal dwReserved As Long, _
ByVal uReturnCode As Long) As Long
ExitWindows 0, 0
ExitWindowsEx function does more than just logging off. It can be called to perform logoff, shutdown, reboot, forced shutdown etc. The first parameter takes one or more of constants as defined below.
Const EWX_LOGOFF = 0 ' Log off the session Const EWX_SHUTDOWN = 1 ' Shuts down to bring "Safe to power off" screen Const EWX_REBOOT = 2 ' Reboots the system Const EWX_FORCE = 4 ' Forcefully carry the above Const EWX_POWEROFF = 8 ' Power off the system, if system supports Const EWX_FORCEIFHUNG = 16 ' Force shutdown if applications donot respond within timeout interval
The second parameter is supported only by Windows XP and above and is ignored by others. This flag is a combination of a major and a minor flag. This flag is used for the purpose of logging the details of shutdown.
'Major Flags Const SHTDN_REASON_MAJOR_APPLICATION = &H40000 'Application issue. Const SHTDN_REASON_MAJOR_HARDWARE = &H10000 'Hardware issue. Const SHTDN_REASON_MAJOR_LEGACY_API = &H70000 'The InitiateSystemShutdown function was 'used instead of InitiateSystemShutdownEx. Const SHTDN_REASON_MAJOR_OPERATINGSYSTEM = &H20000 'Operating system issue. Const SHTDN_REASON_MAJOR_OTHER = &H0 'Other issue. Const SHTDN_REASON_MAJOR_POWER = &H60000 'Power failure. Const SHTDN_REASON_MAJOR_SOFTWARE = &H30000 'Software issue. Const SHTDN_REASON_MAJOR_SYSTEM = &H50000 'System failure. 'Minor Flags Const SHTDN_REASON_MINOR_BLUESCREEN = &HF 'Blue screen crash event. Const SHTDN_REASON_MINOR_CORDUNPLUGGED = &HB 'Unplugged. Const SHTDN_REASON_MINOR_DISK = &H7 'Disk. Const SHTDN_REASON_MINOR_ENVIRONMENT = &HC 'Environment. Const SHTDN_REASON_MINOR_HARDWARE_DRIVER = &HD 'Driver. Const SHTDN_REASON_MINOR_HOTFIX = &H11 'Hot fix. Const SHTDN_REASON_MINOR_HOTFIX_UNINSTALL = &H17 'Hot fix uninstallation. Const SHTDN_REASON_MINOR_HUNG = &H5 'Unresponsive. Const SHTDN_REASON_MINOR_INSTALLATION = &H2 'Installation. Const SHTDN_REASON_MINOR_MAINTENANCE = &H1 'Maintenance. Const SHTDN_REASON_MINOR_MMC = &H19 'MMC issue. Const SHTDN_REASON_MINOR_NETWORK_CONNECTIVITY = &H14 'Network connectivity. Const SHTDN_REASON_MINOR_NETWORKCARD = &H9 'Network card. Const SHTDN_REASON_MINOR_OTHER = &H0 'Other issue. Const SHTDN_REASON_MINOR_OTHERDRIVER = &HE 'Other driver event. Const SHTDN_REASON_MINOR_POWER_SUPPLY = &HA 'Power supply. Const SHTDN_REASON_MINOR_PROCESSOR = &H8 'Processor. Const SHTDN_REASON_MINOR_RECONFIG = &H4 'Reconfigure. Const SHTDN_REASON_MINOR_SECURITY = &H13 'Security issue. Const SHTDN_REASON_MINOR_SECURITYFIX = &H12 'Security patch. Const SHTDN_REASON_MINOR_SECURITYFIX_UNINSTALL = &H18 'Security patch uninstallation. Const SHTDN_REASON_MINOR_SERVICEPACK = &H10 'Service pack. Const SHTDN_REASON_MINOR_SERVICEPACK_UNINSTALL = &H16 'Service pack uninstallation. Const SHTDN_REASON_MINOR_TERMSRV = &H20 'Terminal Services. Const SHTDN_REASON_MINOR_UNSTABLE = &H6 'Unstable. Const SHTDN_REASON_MINOR_UPGRADE = &H3 'Upgrade. Const SHTDN_REASON_MINOR_WMI = &H15 'WMI issue.
If the function succeeds non-zero is returned otherwise zero is returned.
Declare Function ExitWindowsEx Lib "user32" ( _
ByVal uFlags As Long, _
ByVal dwReason As Long) As Long
ExitWindowsEx EWX_REBOOT + EWX_FORCE, _
SHTDN_REASON_MAJOR_SOFTWARE + SHTDN_REASON_MINOR_BLUESCREEN
InitiateSystemShutdown and InitiateSystemShutdownEx functions the same way with the only difference that the later function uses dwReason to log the reason for shutdown, as mentioned above. The functions takes for parameter a machine name, if null or empty string is passed the local computer is shut. The second parameter is the message that is displayed to the user, third parameter is the timeout in seconds for which the system waits for response. Fourth and fifth parameters are boolean for whether shutdown should be forced and if the system is rebooted.
AbortSystemShutdown function aborts the shutting down of the system initiated by functions InitiateSystemShutdown and InitiateSystemShutdownEx. The only parameter is takes is the machine name.
Declare Function InitiateSystemShutdown Lib "advapi32" Alias "InitiateSystemShutdownA" ( _
ByVal lpMachineName As String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Long, _
ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean) As Long
Declare Function InitiateSystemShutdownEx Lib "advapi32" Alias "InitiateSystemShutdownExA" ( _
ByVal lpMachineName As String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Long, _
ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean, _
ByVal dwReason As Long) As Long
Declare Function AbortSystemShutdown Lib "advapi32" Alias "AbortSystemShutdownA" ( _
ByVal lpMachineName As String) As Long

[...] post of interest was System Shutdown Windows API Functions, follwed by Accessing Clipboard from VB. Both posts fetures related Windows API [...]
Pingback by Last 30 days « Jalaj — April 10, 2007 @ 10:45 am
[...] System Shutdown Windows API Functions [...]
Pingback by Just Looking Back « Jalaj — May 28, 2007 @ 6:25 am
thanks for lockworksation().
Comment by Anand — November 29, 2007 @ 10:01 am
Very nice
Comment by Sekar — January 8, 2008 @ 1:21 pm