Anyone know how to ie. start Calc.exe and wait for Calc.exe to close before more code is processed?
To halt the code while it's "sub"-window is running?
Printable View
Anyone know how to ie. start Calc.exe and wait for Calc.exe to close before more code is processed?
To halt the code while it's "sub"-window is running?
Sure, add the following code to a code module:
Now just call ShellAndWait with the program you want to start as the first argument and optional a windowstyle constant as the second.Code: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 CreateProcess _
Lib "kernel32" Alias "CreateProcessA" ( _
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 Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const STARTF_USESHOWWINDOW = &H1
Public Sub ShellAndWait(sCmd As String, Optional WindowStyle As VbAppWinStyle = vbNormalFocus)
Dim udtStart As STARTUPINFO
Dim udtProc As PROCESS_INFORMATION
udtStart.cb = Len(udtStart)
udtStart.dwFlags = STARTF_USESHOWWINDOW
udtStart.wShowWindow = WindowStyle
CreateProcess 0&, sCmd, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc
WaitForSingleObject udtProc.hProcess, INFINITE
CloseHandle udtProc.hProcess
AppActivate Me.Caption
End Sub
ShellAndWait "Calc.exe" or ShellAndWait "Notepad.exe", vbMaximizedFocus
Good luck!
thx :-)
I have this definition
Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
As Long) As Long
and uses this code
I works for me but does it have a flaw ?Code:dblProgramTaskId = Shell(strProgramFileName, vbNormalFocus)
DoEvents
If dblProgramTaskId = 0 Then
WriteToErrFile "Faield to run " & strProgramFileName
Else
Do Until OpenProcess(SYNCHRONIZE, True, dblProgramTaskId) = 0
DoEvents
Loop
End If
Nope no flaws as far as I know. But the SYNCHRONIZE value is only (as far as I know) supported on WinNT and Win2000.
I think I will try the 2nd solution there. The 1st causes lockup and the reason is pasted here: ( i am using DDE )
Use caution when calling the wait functions and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. A thread that uses a wait function with no time-out interval may cause the system to become deadlocked. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForSingleObject.
Should try to make a version of the function useing WaitForMultipleObjects. That way my DDE proggie dont hang.