VB Quicktakes - Log Off Windows

This code can be used to shut down, restart, or logoff of windows depending on which constants you supply. Not all methods work with different versions of windows and also may not work if you don't have the right privleges. I chose the logoff flag because it should work under all circumstances.

Option Explicit

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_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_POWEROFF = 8

Public Sub LogOffWindows() Dim frm As Form ExitWindowsEx EWX_LOGOFF, 0 For Each frm In Forms Unload frm Next End Sub
About this page: