How to Use ExitWindowsExec() in VB to Run MS-DOS Batch File

This article was previously published under Q147806
This article has been archived. It is offered "as is" and will no longer be updated.
The ExitWindowsExec() function terminates Windows, runs a specified MS-DOSapplication, and then restarts Windows. The information in this articleshows you how to call this function from Microsoft Visual Basic.
More information
The ExitWindowsExec() function is typically used by installation programsto replace components of Windows that are active when Windows is running.Normally, you'd want to run an MS-DOS batch file that performs the filecopying while Windows is temporarily shut down. The declaration forExitWindowsExec() is as follows:
   ' Place the following declaration on one, single line:  Private Declare Function ExitWindowsExec Lib "User" (ByVal lpszExe As String,      ByVal lpszParams As Any) As Integer				

First Parameter: lpszExe$

The first parameter for ExitWindowsExec(), lpszExe$, should be a stringcontaining the fully qualified path to the executable file you want to run.This string must contain no more than 127 characters. For batch files,you'll need to specify COMMAND.COM as the file -- C:\DOS\COMMAND.COM. Toget the fully qualified path in Visual Basic to COMMAND.COM, you can usethe Environ$ function:
   lpszExe$ = Environ$("COMSPEC")				

For more information on the Environ$ function, please refer to theMicrosoft Visual Basic Language Reference or the Help menu.

Second Parameter: lpszParams$

The second parameter for ExitWindowsExec(), lpszParams$, should be a stringcontaining any necessary parameters for the executable file. If noparameters are necessary, pass a long integer 0 such as 0&.

To execute a batch file, however, this is where you specify the path to thebatch file and any parameters it needs. Also, you need to preface thestring with the /c switch which tells MS-DOS to invoke a copy ofCOMMAND.COM. Here is an example:

The return value of this function is False when the function fails.

Step-by-Step Example

  1. Using either NotePad in Windows or Edit in MS-DOS, create a batch file in the root directory of your hard disk called C:\RUNIT.BAT. Give it the following contents:
       @echo off   echo Making Backup of autoexec.bat   copy c:\autoexec.bat c:\*.bak   echo Done						
  2. Start a new project in Visual Basic (Alt, F, N). Form1 is created by default.
  3. Add the following declaration to the General Declarations section of the form:
       ' Place the following declaration on one, single line:   Private Declare Function ExitWindowsExec Lib "User" (ByVal lpszExe As String, _      ByVal lpszParams As Any) As Integer						
  4. Add a command button (Command1) to the form, and place the following code in the Click() event.
       Private Sub Command1_Click ()      sComspec$ = Environ$("COMSPEC")      ret% = ExitWindowsExec(sComspec$,  "/c c:\runit.bat")   End Sub						
  5. Save the project (Alt, F, V). Then press the F5 key to run the program. Click the command button to exit Windows, run the batch file, and then restart Windows.
vbwin vb416 4.00

Article ID: 147806 - Last Review: 11/01/2013 22:18:00 - Revision: 2.0

Microsoft Visual Basic 4.0 Professional Edition

  • kbnosurvey kbarchive kbwndw KB147806