Visual Basic > Windows and Controls
Shutdown restart logoff the local machine
Shutdown restart logoff the local machine 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 '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. 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 'Notes : DOESN'T REQUIRE ANY PRIVILAGES 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
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows