Microsoft KB Archive/76981

= VB3: HOWTO: Terminate Windows from a Visual Basic Application =

Article ID: 76981

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q76981



SUMMARY
The Visual Basic SendKeys function cannot be used to close Program Manager in order to terminate 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 will 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 will never arrive in the dialog box.

To correctly close Program Manager, you must use the ExitWindows API function. You can declare this API function in the GLOBAL.BAS module. For example:

 Start a new project in Visual Basic. Draw a command button on the form.  Add the following as a single line to GLOBAL.BAS: Declare Function ExitWindows Lib &quot;user&quot; (ByVal wReturnCode as Long,     ByVal dwReserved as Integer) as Integer

  Add the following line of code to the Command button's Click procedure: RetVal% = ExitWindows(0,0)

 Save the file and then run the program. Click the command button.</ol>

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 and handle it appropriately.

Steps to Reproduce Behavior
<ol> Start a New Project in Visual Basic.</li> Draw a Command button on the form.</li>  In the Command button Click event procedure, add this code: AppActivate(&quot;Program Manager&quot;) SendKeys &quot;%{ }{DOWN 5}{ENTER 2}&quot;, 0 'ALT, SPACE, DOWN 5, ENTER 2

</li> Save the file and then run the program.</li></ol>

The Program Manager does not close. If you choose the OK button with the mouse, you'll see a message stating, &quot;Can't quit at this time.&quot; If you choose the Cancel button, you'll see a message stating, &quot;Cannot start more than one copy of the specified program.&quot; These messages are misleading, but are the result of attempting an unsupported action.

Additional query words: 2.00 3.00 restart start exit windows

Keywords: kbhowto kbwndw KB76981

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.