VB and VBA Users Source Code: Shutdown/restart/logoff the local machine
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Shutdown/restart/logoff the local machine
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, February 06, 2001
Hits:
949
Category:
Windows API
Article:
The following API Declares and Constants can be used to force a PC to restart. Note, a demonstration routine can be found at the end of this post. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags as Long, ByVal wReserved as Long) as Long Private Const EWX_FORCE = 4 'Force any applications to quit instead of prompting the user to close them. Private Const EWX_POWEROFF = 8 'Shut down the system and, if possible, turn the computer off. Private Const EWX_LOGOFF = 0 'Log off the network Private Const EWX_REBOOT = 2 'Perform a full reboot of the system Private Const EWX_SHUTDOWN = 1 'Shut down the system 'Purpose : Forces a machine shut down/logoff 'Inputs : [lShutDownType] If unspecified, will reboot the machine else one of the above constants 'Outputs : If successful returns a number other than zero 'Author : Andrew Baker 'Date : 31/01/2001 14:35 'Notes : If this fails check the value of Err.LastDllError. If this is 1314 then ' the you do not have sufficient privileges to call this function. 'Revisions : Function MachineShutDown(Optional lShutDownType As Long = EWX_REBOOT) As Long MachineShutDown = ExitWindowsEx(lShutDownType, 0&) 'shut down the computer End Function 'Purpose : Forces a machine shut down/logoff 'Inputs : N/A 'Outputs : N/A 'Author : Andrew Baker 'Date : 13/11/2002 10:14 'Notes : DOESN'T REQUIRE ANY PRIVILAGES 'Revisions : Sub MachineShutDown2() Shell "rundll32.exe user32.dll, ExitWindowsEx", vbHide End Sub 'Demonstration routine Sub Test() 'Forces the computer to reboot 'When the EWX_FORCE flag is set, Windows does not send the messages WM_QUERYENDSESSION and 'WM_ENDSESSION to the applications currently running in the system. This can cause the 'applications to lose data. Therefore, you should only use this flag in an emergency. Call MachineShutDown(EWX_FORCE Or EWX_REBOOT) 'Forces the user to log off Call MachineShutDown(EWX_LOGOFF) 'Shuts the computer down (no restart) Call MachineShutDown(EWX_SHUTDOWN) 'Shuts the computer down and turns power off (if possible) Call MachineShutDown(EWX_POWEROFF) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder