HOWTO: Terminate Windows from a Visual Basic Application

Last reviewed: October 13, 1997
Article ID: Q142820
The information in this article applies to:
  • Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0

SUMMARY

The Microsoft Visual Basic for Windows SendKeys function cannot be used to close the Microsoft Windows Program Manager in order to terminate Microsoft Windows. To correctly close Program Manager, you must invoke the ExitWindows API function, as shown below.

Many software setup or installation programs are designed to exit Windows, and then restart Windows when the setup or installation is complete. You can make a Visual Basic program automatically exit Windows and then restart Windows by passing the EW_RESTARTWINDOWS value to the ExitWindows API function. The value for the EW_RESTARTWINDOWS constant is &H42.

MORE INFORMATION

You may want to terminate the current Windows session by closing the Program Manager from within a Visual Basic application. You may think that you can activate the Program Manager control menu and send the appropriate key sequences using the Visual Basic SendKeys function. However, this method does not work because after the Close menu item is chosen, a system modal dialog box is opened that prompts you to save changes to Program Manager. A system modal dialog box locks out ALL other programs until it is satisfied. Therefore, the keystroke you send by using the SendKeys function never arrives in the dialog box.

To correctly close Program Manager, you must use the ExitWindows API function. You can declare this API function in a code module name Module1.

Step-by-Step Example

  1. Start a new project in Visual Basic.

  2. Draw a Command button on the form.

  3. Add the following as a single line to Module1:

       ' Use for Windows 3.11 or Windows for Workgroups 3.11 applications:
          Declare Function ExitWindows Lib "user" (ByVal wReturnCode as Long, _
             ByVal dwReserved as Integer) as Integer
    
       ' Use for Windows 95 or Windows NT applications:
          Declare Function ExitWindows Lib "user32" alias "ExitWindowsEx" _
             (ByVal wReturnCode as Long, ByVal dwReserved as Long) as Long
    
    

  4. Add the following line of code to the command button's Click procedure:

       ' Use for Windows 3.11 or Windows for Workgroups 3.11 applications:
          RetVal% = ExitWindows(0,0)
    
       ' Use for Windows 95 or Windows NT Applications:
          RetVal& = ExitWindows(0&, 0&)
    
    

  5. Run the program.

  6. Click the Command button.

The ExitWindows API call initiates the standard Windows shutdown procedure. If all applications agree to terminate, the windows session is terminated and control returns to MS-DOS. If the ExitWindows API call fails due to an open MS-DOS session or for some other reason, FALSE is returned. You should check for this return value and handle it appropriately.

Steps to Reproduce Behavior

  1. Start a new Project in Visual Basic.

  2. Draw a Command button on the form.

  3. In the Command button Click event procedure, add this code:

    AppActivate("Program Manager") SendKeys "%{ }{DOWN 5}{ENTER 2}", 0 'ALT, SPACE, DOWN 5, ENTER 2

  4. Run the program.

The Program Manager does not close. If you choose the OK button with the mouse, a message is displayed stating, "Can't quit at this time." If you choose the Cancel button, a message is displayed stating, "Cannot start more than one copy of the specified program." These messages are misleading, but are the result of attempting an unsupported action.
Keywords          : APrgOther APrgWindow vb416 VB4WIN kbui
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.