Wait for shelled process and all child processes

386 views Asked by At

Apologies is this has already been answered, I cannot find it. I have a requirement to launch an external process from a vb6 app and wait for that process to finish before continuing. Simple enough. However the process I need to launch in turn launches a child process then exits. I need to wait for the child process to complete (and and other child processes)

Existing code:

Private Const WAIT_INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

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

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

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

Public Sub ShellProcess(strProcess As String, Optional blnWait As Boolean = False)
   Dim hProc As Long
   Dim taskId As Long
   Dim cmdline As String

    cmdline = strProcess
    taskId = Shell(cmdline, vbNormalFocus)

    If blnWait = True Then
         hProc = OpenProcess(SYNCHRONIZE, True, taskId)
        Call WaitForSingleObject(hProc, WAIT_INFINITE)
        CloseHandle hProc
    End If

    MsgBox "The shelled app has ended."
End Sub

I have managed to do this in c# sometime ago but now have only vb6 to work with.

0

There are 0 answers