Call an Exe and wait until it is executed

Posted on 2003-02-28
Medium Priority
Last Modified: 2010-08-05
I have written a small program/EXE in .NET (which does some image preprocessing).  What I need is, I want to call this from my VB 6.0 application (which does a lot of other things).  If I call the EXE using the "Shell" function, it will run the EXE; but it will not wait till the execution is finished.  It will go to the next statement.

Is there any other way to call an EXE which will wait until the execution of the EXE is over and then go to the next statement ?
Question by:benvarg
  • 2
LVL 70

Accepted Solution

Éric Moreau earned 80 total points
ID: 8042243
You can use this:

Option Explicit

Private Declare Function WaitForSingleObject _
                 Lib "kernel32" _
                 (ByVal hHandle As Long, _
                 ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess _
                 Lib "kernel32" _
                 (ByVal dwDesiredAccess As Long, _
                 ByVal bInheritHandle As Long, _
                 ByVal dwProcessId As Long) As Long

Private Const INFINITE = &HFFFF

Private Sub Command1_Click()
    WaitForProcessToEnd "CALC.EXE"
End Sub

Private Function WaitForProcessToEnd(ByVal pstrExecute As String) As Boolean
'*** Execute a process an wait for it to end before continuing here ***
Dim lngHandle As Long
Dim lngProcessID As Long
Dim lngReturnValue As Long

    On Error GoTo ErrorHandler
    WaitForProcessToEnd = False
    lngProcessID = Shell(pstrExecute)
    lngHandle = OpenProcess(&H100000, True, lngProcessID)
    lngReturnValue = WaitForSingleObject(lngHandle, INFINITE)
    MsgBox "Here is the proof that I was waiting!!!"
    WaitForProcessToEnd = True
    Exit Function
    Select Case Err.Number
        Case 53  'File not found
            MsgBox pstrExecute & " cannot be found.", vbOKOnly + vbCritical
            Resume GoHere
        Case Else
            MsgBox Err.Number & " : " & Err.Description
            Resume GoHere
    End Select
End Function


Author Comment

ID: 8042340
Hi emoreau,

That is good.  I just tried it as it is.  Now let me try with my actual application.


Author Comment

ID: 8047435
Thanks emoreau.  You gave me the complete code.  It is what I wanted.  Thank You

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question