Shutdown, restart or log off

Using the ExitWindowsEx API call, you can tell windows to shutdown, restart of log off:

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const EWX_FORCE = 4
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2
Private Const EWX_SHUTDOWN = 1

Private Sub cmdShutDown()
    ExitWindowsEx EWX_SHUTDOWN, 0
End Sub

You can also make the PC log off, reboot or force a log off (without giving the user a chance to save his/her files), by using the other constants instead of EWX_SHUTDOWN. Note that in Windows 2000 (maybe NT 4 too), EWX_SHUTDOWN and EWX_REBOOT do not work. I presume this is a security measure so that virus writers cannot turn of a critical PC. EWX_LOGOFF and EWX_FORCE still work.

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”