Synchronizing ShellExecute
I've looked all over the net trying to find a way to synchronize a program executed with ShellExecute, and all the code I've found fails. For some reason, any attempt to run my self-terminating command-line program, such as CreateProcessA and Shell fails, and only ShellExecute really works. The following code is the closest I could get to it, but CreateProcess fails, and ShellExecute doesn't return a handle:
VB Code:
'Declarations are omited to make it easier to read.
Public Function ShellSynchronous(cmdline As String) As Long
On Error GoTo ProcErr
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
Dim Count As Long
start.cb = Len(start)
ret = CreateProcessA(0, cmdline, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, start, proc)
Count = GetTickCount
ret = WaitForSingleObject(proc.hProcess, INFINITE)
'If (GetTickCount - Count) < 1000 Then
' Sleep 1000
'End If
Call GetExitCodeProcess(proc.hProcess, ret)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ShellSynchronous = ret
Exit Function
ProcErr:
Err.Raise Err.Number
End Function
Re: Synchronizing ShellExecute
You mean you want your code to wait until the executed application finishes?
Search for ShellAndWait. It does exactly that.
Re: Synchronizing ShellExecute
Thanks, but as I mentioned previously, for some reason the shelled program fails when it's called by any means other than ShellExecute. ShellWait is the same code I posted above, which uses CreateProcess to run the DOS program. It might have relevance that the program takes command line parameters.