What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleShutdown Windows
DescriptionThis example shows how to shutdown Windows in Visual Basic 6 by using the ExitWindows or ExitWindowsEx API function.
KeywordsExitWindows, shutdown, reboot
Use the ExitWindowsEx or ExitWindows API functions.

Note that other applications can prevent normal shutdown. For example, if the user is editing a file, the program may ask the user whether it should save the changes. If the user cancels, the program will stop the shutdown.

This technique seems to work more or less in Win 3.x, Windows 95, Windows NT, and Windows 98. Let me know if you find that it does not work on your system.

#If Win32 Then
    Private Declare Function Shutdown Lib "user32" Alias _
        "ExitWindowsEx" (ByVal uFlags As Long, ByVal _
        dwReserved As Long) As Long
    Private Const EWX_LOGOFF = 0
    Private Const EWX_SHUTDOWN = 1
    Private Const EWX_REBOOT = 2
    Private Const EWX_FORCE = 4
    Private Declare Function Shutdown Lib "User" Alias _
        "ExitWindows" (ByVal dwReturnCode As Long, ByVal _
        wReserved As Integer) As Integer
    Private Const EW_REBOOTSYSTEM = &H43
    Private Const EW_RESTARTWINDOWS = &H42
#End If

Private SelectedOption As Integer

Private Sub Form_Load()
    #If Win32 Then
        ' Prepare for 32 bit ExitWindowsEx.
        optExitOption(0).Caption = "Normal Shutdown"
        optExitOption(0).Tag = EWX_SHUTDOWN

        optExitOption(1).Caption = "Reboot"
        optExitOption(1).Tag = EWX_REBOOT
        optExitOption(2).Caption = "Log Off"
        optExitOption(2).Tag = EWX_LOGOFF
        optExitOption(3).Caption = "Forced Shutdown"
        optExitOption(3).Tag = EWX_FORCE
        ' Prepare for 16 bit ExitWindows.
        optExitOption(0).Caption = "Normal Shutdown"
        optExitOption(0).Tag = 0

        optExitOption(1).Caption = "Reboot"
        optExitOption(1).Tag = EW_REBOOTSYSTEM

        optExitOption(2).Caption = "Restart Windows"
        optExitOption(2).Tag = EW_RESTARTWINDOWS
        optExitOption(3).Visible = False
    #End If
End Sub

Private Sub cmdOk_Click()
Dim exit_option As Long

    exit_option = CLng(optExitOption(SelectedOption).Tag)
    Shutdown exit_option, 0
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.