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:
  1. 'Declarations are omited to make it easier to read.
  2. Public Function ShellSynchronous(cmdline As String) As Long
  3. On Error GoTo ProcErr
  4.     Dim proc As PROCESS_INFORMATION
  5.     Dim start As STARTUPINFO
  6.     Dim ret As Long
  7.     Dim Count As Long
  8.    
  9.     start.cb = Len(start)
  10.     ret = CreateProcessA(0, cmdline, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, start, proc)
  11.     Count = GetTickCount
  12.     ret = WaitForSingleObject(proc.hProcess, INFINITE)
  13.     'If (GetTickCount - Count) < 1000 Then
  14.     '    Sleep 1000
  15.     'End If
  16.     Call GetExitCodeProcess(proc.hProcess, ret)
  17.     Call CloseHandle(proc.hThread)
  18.     Call CloseHandle(proc.hProcess)
  19.     ShellSynchronous = ret
  20.  
  21.     Exit Function
  22.  
  23. ProcErr:
  24.     Err.Raise Err.Number
  25.  
  26. End Function