
Wait for "Shell"-ed app to finish
Quote:
>How can make sure that my procedure won't continue until the application I
>start has completed?
>Thanks
You need to use the OpenProcess() Api call to shell your program rather than
the Vb Shell Function. This
returns the handle to the shelled process for use by the WaitForSingleObject
API call.
I posted the following sub in response to a similar problem some weeks back;
the sub wraps this up the above API functions:
' *****Declarations*****
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&
Public Const SW_SHOW& = 5 'sets nShowCmd param in ShellExecute API function
Public Const SW_HIDE& = 0
Public Const STARTF_USESHOWWINDOW& = &H1
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
'*****End Of Declarations*****
Public Sub OpenProcess(cmdline$) 'takes path to shelled app as a parameter
Dim start As STARTUPINFO
Dim proc As PROCESS_INFORMATION
Dim lngreturn As Long
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
start.dwFlags = STARTF_USESHOWWINDOW 'forces functn to use wShowWindow
param below
start.wShowWindow = SW_HIDE 'shelled process is HIDDEN. Use SW_SHOW etc
to run visible
' Start the shelled application:
lngreturn = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
lngreturn = WaitForSingleObject(proc.hProcess, INFINITE)
lngreturn = CloseHandle(proc.hProcess)
End Sub
See Q129796 in the Knowledge Base for further details of this.