
How can I wait until a program finishes ?
You can place the following into a module.
When you call the "ExecuteAndWait" function pass one of the "SW_" constants
to indicate how you want the program's window to appear when it is
launched.
You might want to do a little reading on the "CreateProcess",
"WaitForSingleObject", and "CloseHandle" methods in order to add your own
error handling code to the function for those calls when unexpected values
are received in the local *lReturn* variable.
----------------------------------------------------------
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
Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&
Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long,
ByVal dwMilliseconds As Long) As Long
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
Global Const STARTF_USESHOWWINDOW = 1
Public Function ExecuteAndWait(sExecFileName As String, iShowCommand As
Integer) As Integer
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim lReturn As Long
NameStart.cb = Len(NameStart)
NameStart.dwFlags = STARTF_USESHOWWINDOW
NameStart.wShowWindow = iShowCommand
lReturn = CreateProcessA(0&, sExecFileName, 0&, 0&, 1&,
NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
lReturn = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
lReturn = CloseHandle(NameOfProc.hProcess)
ExecuteAndWait = lReturn
End Function