Visual Basic FAQ

GetExitCodeProcess: Determine when a Shelled App has Ended
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6


GetExitCodeProcess: Determine when a Shelled App has Ended
WaitForSingleObject: Determine when a Process has Ended

WaitForSingleObject: Determine when a Shelled App has Ended

In a Visual Basic application, once a call to the VB Shell( ) function has been made, program execution continues on immediately. However, there are times when it may be desirable or necessary to wait for the shelled application to close before continuing. By utilizing the OpenProcess and GetExitCodeProcess APIs under Win32, the  procedure below will 'hold' the application in a Do...Loop loop, checking for the shelled app's termination.

Thanks go out to Santiago Holgado for locating the proper "active status flag" for the routine.

 BAS Module Code

 Form Code
Add a command button to a form along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, _
   ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
  (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long

Private Const STATUS_PENDING = &H103&

Private Sub Command1_Click()

   RunShell "c:\windows\notepad.exe"

End Sub

Private Sub RunShell(cmdline As String)

    Dim hProcess As Long
    Dim ProcessId As Long
    Dim exitCode As Long

    ProcessId = Shell(cmdline, 1)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)


        Call GetExitCodeProcess(hProcess, exitCode)
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

    MsgBox "The shelled process " & cmdline & " has ended."

End Sub
Run the app, and click the command button. Notepad will launch, and only when closed will the message box will appear.

During the loop, it is imperative that a DoEvents statement be used. The DoEvents statement relinquishes your application's control
to allow Windows to process any pending messages or events for your app or any other running process. Without this, your application (and Windows 3) will appear to lock up as the While...Wend loop essentially 'grabs control' of the system. To demonstrate this, comment-out the DoEvents line, and  run the app. Once Notepad starts, the system will appear to lock up. You will need to press CTRL + Break to stop the routine and regain system control.

Win32 APIs are not available under 16 bit Visual Basic. For details on waiting for a shelled app using VB2/3 or 4-16 bit, see GetModuleUsage: Determine when a Shelled App has Ended (16-bit).


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter