CreateProcess API assistance

I need to be able to launch a .bat file from a VB application and wait for that batch file to complete before returning control back to the VB program.

I understand that this can be accomplished using the CreateProcess API but am having a hard time tracking down some examples.
Would be appreciated if someone could post some example code for me to look into.

Many thanks in advance :)
LVL 5
juliancrawfordAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
you can use this code:

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
   
GoHere:
    Exit Function
   
ErrorHandler:
    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

0
 
juliancrawfordAuthor Commented:
Just what I was after - thanks
0
All Courses

From novice to tech pro — start learning today.