Function WinExecAndWait32V2( FileName: String; Visibility: integer ): DWORD;<br> Procedure WaitFor( processHandle: THandle );<br> Var<br> msg: TMsg;<br> ret: DWORD;<br> Begin<br> Repeat<br> ret := MsgWaitForMultipleObjects(<br> 1, { 1 handle to wait on }<br> processHandle, { the handle }<br> False, { wake on any event }<br> INFINITE, { wait without timeout }<br> QS_PAINT or { wake on paint messages }<br> QS_SENDMESSAGE { or messages from other threads }<br>  
;<br> If ret = WAIT_FAILED Then Exit; { can do little here }<br> If ret = (WAIT_OBJECT_0 + 1) Then Begin<br> { Woke on a message, process paint messages only. Calling<br> PeekMessage gets messages send from other threads processed. }<br> While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do<br> DispatchMessage( msg );<br> End;<br> Until ret = WAIT_OBJECT_0;<br> End; { Waitfor }<br>Var { V1 by Pat Ritchey, V2 by P.Below }<br> zAppName:array[0..512] of char;<br> StartupInfo:TStartupInfo;<br> ProcessInfo:TProcessInformation;<br>Begin { WinExecAndWait32V2 }<br> StrPCopy(zAppName,FileName);<br> FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br> StartupInfo.cb := Sizeof(StartupInfo);<br> StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br> StartupInfo.wShowWindow := Visibility;<br> If not CreateProcess(nil,<br> zAppName, { pointer to command line string }<br> nil, { pointer to process security attributes }<br> nil, { pointer to thread security attributes }<br> false, { handle inheritance flag }<br> CREATE_NEW_CONSOLE or { creation flags }<br> NORMAL_PRIORITY_CLASS,<br> nil, { pointer to new environment block }<br> nil, { pointer to current directory name }<br> StartupInfo, { pointer to STARTUPINFO }<br> ProcessInfo) { pointer to PROCESS_INF }<br> Then<br> Result := DWORD(-1) { failed, GetLastError has error code }<br> Else Begin<br> Waitfor(ProcessInfo.hProcess);<br> GetExitCodeProcess(ProcessInfo.hProcess, Result);<br> CloseHandle( ProcessInfo.hProcess );<br> CloseHandle( ProcessInfo.hThread );<br> End; { Else }<br>End; { WinExecAndWait32V2 }