
Waiting for an application to finish executing before executing next app
Hi Lars,
The setup.exe that is generated by InstallShield is a 16 bit application so
WaitForSingleObject will not work because all 16 bit applications run in
the same process space. Also, there is another problem with InstallShield.
On launching the setup.exe , a setup engine is started. This is when the
progress bar starts from 0% to 100% starts. Moment the progress bar
finishes 100% , it instantias the setup wizard (or the Blue or green screen
in lay-man's language) and the setup engine terminates . So you can see
there are actually two process running one after the other. To put in other
words , for each application , there are two process running one after the
other when you click on setup.exe. So even you can get it work in VB and
use WaitForSingleObject to detect when it terminates, the
WaitForSingleObject will return when the first process(setup.exe) returns.
To workaround the above two problems, you can try the following code and
see if it works on your side.
Private 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
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadId As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle
As Long, ByVal dwMilliseconds As Long) As Long
Private 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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess
As Long, lpExitCode As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS,
0&, 0&, start, proc)
' Wait for the shelled application to finish:
'ret& = WaitForSingleObject(proc.hThread, INFINITE)
'Call GetExitCodeProcess(proc.hThread, ret&)
'Call CloseHandle(proc.hThread)
Do
ret& = WaitForSingleObject(proc.hProcess, 0)
DoEvents
Loop Until ret& <> 258
ret& = CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function
Sub Form_Click()
Dim RetVal As Long
RetVal = ExecCmd("D:\My Installations\TestApp\Media\TestApp\Disk
Images\disk1\setup.exe")
WaitForInstallShieldToStop "InstallShield_Win"
MsgBox "Process Finished, Exit Code " & RetVal
End Sub
Private Sub WaitForInstallShieldToStop(strClassName As String)
Dim lngFoundhWnd As Long
On Error GoTo WaitForInstallShieldToStop_EH
' Usage:
' WaitForInstallShieldToStop "ISINSTALLSCLASS"
lngFoundhWnd = FindWindow(strClassName, vbNullString)
Do Until lngFoundhWnd = 0
DoEvents
Sleep 100
lngFoundhWnd = FindWindow(strClassName, vbNullString)
Loop
Exit Sub
WaitForInstallShieldToStop_EH:
Exit Sub
End Sub
Best Regards,
Elan Zhou