如何在一个应用程序当中判断某个BAT或EXE文件是否正在被执行(50分)

  • 主题发起人 sentiment
  • 开始时间
S

sentiment

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序1中用SHELLEXECUTE函数运行某个BAT或者EXE文件(例如2.EXE),我需要是程序1在执行<br>SHELLEXECUTE后就等待,知道2.exe文件运行结束。如何实现,请高手指教。
 
function WinExecAndWait32(FileName:String; Visibility : integer):integer; <br>var <br>&nbsp; zAppName:array[0..512] of char; <br>&nbsp; zCurDir:array[0..255] of char; <br>&nbsp; WorkDir:String; <br>&nbsp; StartupInfo:TStartupInfo; <br>&nbsp; ProcessInfo:TProcessInformation; <br>begin <br>&nbsp; StrPCopy(zAppName,FileName); <br>&nbsp; GetDir(0,WorkDir); <br>&nbsp; StrPCopy(zCurDir,WorkDir); <br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0); <br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo); <br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW; <br>&nbsp; StartupInfo.wShowWindow := Visibility; <br>&nbsp; if not CreateProcess(nil, <br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ pointer to command line string } <br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to process security attributes } <br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to thread security attributes } <br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { handle inheritance flag } <br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ creation flags } <br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS, <br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to new environment block } <br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to current directory name } <br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to STARTUPINFO } <br>&nbsp; &nbsp; ProcessInfo) then Result := -1 { pointer to PROCESS_INF } <br>&nbsp; else begin <br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE); <br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess,Result); <br>&nbsp; end; <br>end; <br><br><br>Function tform1.WinExecAndWait32V2( FileName: String; Visibility: integer ): DWORD;<br>&nbsp; Procedure WaitFor( processHandle: THandle );<br>&nbsp; &nbsp; Var<br>&nbsp; &nbsp; &nbsp; msg: TMsg;<br>&nbsp; &nbsp; &nbsp; ret: DWORD;<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; ret := MsgWaitForMultipleObjects(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1, { 1 handle to wait on }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;processHandle, { the handle }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False, { wake on any event }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INFINITE, { wait without timeout }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QS_PAINT or { wake on paint messages }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QS_SENDMESSAGE { or messages from other threads }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);<br>&nbsp; &nbsp; &nbsp; &nbsp; If ret = WAIT_FAILED Then Exit; { can do little here }<br>&nbsp; &nbsp; &nbsp; &nbsp; If ret = (WAIT_OBJECT_0 + 1) Then Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Woke on a message, process paint messages only. Calling<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PeekMessage gets messages send from other threads processed. }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DispatchMessage( msg );<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; Until ret = WAIT_OBJECT_0;<br>&nbsp; &nbsp; End; { Waitfor }<br>&nbsp; Var { V1 by Pat Ritchey, V2 by P.Below }<br>&nbsp; &nbsp; zAppName:array[0..512] of char;<br>&nbsp; &nbsp; StartupInfo:TStartupInfo;<br>&nbsp; &nbsp; ProcessInfo:TProcessInformation;<br>&nbsp; Begin { WinExecAndWait32V2 }<br>&nbsp; &nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; &nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; &nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; &nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; &nbsp; If not CreateProcess(nil,<br>&nbsp; &nbsp; &nbsp; zAppName, { pointer to command line string }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to process security attributes }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to thread security attributes }<br>&nbsp; &nbsp; &nbsp; false, { handle inheritance flag }<br>&nbsp; &nbsp; &nbsp; CREATE_NEW_CONSOLE or { creation flags }<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; &nbsp; nil, { pointer to new environment block }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to current directory name }<br>&nbsp; &nbsp; &nbsp; StartupInfo, { pointer to STARTUPINFO }<br>&nbsp; &nbsp; &nbsp; ProcessInfo) { pointer to PROCESS_INF }<br>&nbsp; &nbsp; Then<br>&nbsp; &nbsp; &nbsp; Result := DWORD(-1) { failed, GetLastError has error code }<br>&nbsp; &nbsp; Else Begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Waitfor(ProcessInfo.hProcess);<br>&nbsp; &nbsp; &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess, Result);<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( ProcessInfo.hProcess );<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( ProcessInfo.hThread );<br>&nbsp; &nbsp; End; { Else }<br>&nbsp; End; { WinExecAndWait32V2 }<br><br>
 
接受答案了.
 
顶部