使用winexec调用外部程序,如何判断程序的结束(100分)

  • 主题发起人 主题发起人 ispeed
  • 开始时间 开始时间
I

ispeed

Unregistered / Unconfirmed
GUEST, unregistred user!
我用winexec运行了一个外部程序,现在我要在那个程序执行完毕之后,<br>才让我的程序再往下执行,即让我的程序wait,如何做到啊?<br><br>下面是我写的代码,但是不行,请大虾们指正:<br>procedure TFrmMain.WinExecAndWait(FilePath :String);<br>var<br>&nbsp;InstanceID :THandle;<br>begin<br>&nbsp; InstanceID:=winexec(PChar(FilePath),SW_HIDE);<br>&nbsp; if InstanceID&lt;32 then OnHandle(3)<br>&nbsp; else<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.ProcessMessages();<br>&nbsp; &nbsp; until Application.Terminated or (GetModuleUsage(InstanceID)=0);<br>end; &nbsp; &nbsp; &nbsp; &nbsp;
 
function Run(sCommandLine: string): Boolean; //Time out when 1 hour elapsed<br>var<br>&nbsp; lpStartupInfo: TStartupInfo;<br>&nbsp; lpProcessInformation: TProcessInformation;<br>begin<br>&nbsp; FillChar(lpStartupInfo, Sizeof(TStartupInfo), #0);<br>&nbsp; lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; lpStartupInfo.wShowWindow := SW_SHOW;<br>&nbsp; Result := CreateProcess(nil, PChar(sCommandLine),<br>&nbsp; &nbsp; &nbsp; nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,<br>&nbsp; &nbsp; &nbsp; lpStartupInfo, lpProcessInformation) and<br>&nbsp; &nbsp; (WaitForSingleObject(lpProcessInformation.hProcess, 3600000) &lt;&gt; WAIT_FAILED);<br>end;<br><br><br>-----<br>http://www.8421.org
 
我看过了,这样是不是要等一个小时它才会告诉你程序结束了<br>
 
不是这意思!<br>是一直等直到它结束或者过了一个小时,你可以自己设置timeout时间
 
function WinExecAndWait32(FileName:String;<br>&nbsp; Parameter: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>&nbsp; //exitcode : Cardinal;<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(zAppName,<br>&nbsp; &nbsp; pChar(Parameter), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to command line string }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; { pointer to process security attributes }<br>&nbsp; &nbsp; nil, &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;{ handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &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; zCurDir, &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<br>&nbsp; &nbsp; &nbsp; Result := -1 { pointer to PROCESS_INF }<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; while (WaitforSingleObject(ProcessInfo.hProcess,5) = WAIT_TIMEOUT) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; application.ProcessMessages;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; //WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; //GetExitCodeProcess(ProcessInfo.hProcess,exitcode);<br>&nbsp; &nbsp; //Result := exitcode;<br>&nbsp; &nbsp; Result := 0;<br>&nbsp; end;<br>end;<br><br>如果你一定要用WinExec,那么就用FindWindow循环检测.
 
赞同qdyoung
 
后退
顶部