如何等待运行的外部程序的关闭?(50分)

  • 主题发起人 主题发起人 CoCo_
  • 开始时间 开始时间
C

CoCo_

Unregistered / Unconfirmed
GUEST, unregistred user!
用Shellexecute执行一个外部程序,我要等待这个外部程序关闭才做进一步的动作,该如何<br>做[?]谢谢!
 
//转载<br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; ProcessInfo: TProcessInformation; &nbsp; // holds process information<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; StartUpInfo: TStartUpInfo; &nbsp; // holds startup information<br>begin<br>&nbsp; {initialize the startup information}<br>&nbsp; FillChar(StartupInfo, SizeOf(TStartupInfo), 0);<br>&nbsp; with StartupInfo do<br><br>&nbsp; begin<br>&nbsp; &nbsp; cb := SizeOf(TStartupInfo);<br>&nbsp; &nbsp; dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; wShowWindow := SW_SHOWNORMAL;<br>&nbsp; end;<br><br>&nbsp; {launch a process}<br>&nbsp; CreateProcess('c:/Windows/calc.exe', nil, nil, nil, False,<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; ExitCode: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds the process exit code<br><br>begin<br>&nbsp; {terminate the process and retrieve the exit code}<br>&nbsp; TerminateProcess(ProcessInfo.HProcess, 10);<br>&nbsp; GetExitCodeProcess(ProcessInfo.HProcess, ExitCode);<br><br>&nbsp; {display the exit code}<br>&nbsp; Label1.Caption := 'The exit code is '+Inttostr(ExitCode);<br>end;<br><br>
 
给你一个function<br>function WinExecAndWait32(FileName: string; Visibility: integer): Cardinal;<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, zAppName, nil, nil, true,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, nil, StartupInfo, ProcessInfo) then<br>&nbsp; &nbsp; Result := INFINITE<br>&nbsp; else begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess, INFINITE);<br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess, Result);<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hProcess);<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hThread);<br>&nbsp; end;<br>end;<br><br>例子:<br>WinExecAndWait32('RegEdit.exe', SW_Show);//当Regedit退出后才会执行下面的语句<br>.................
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=564829<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; ProcessInfo: TProcessInformation; &nbsp; // holds process information<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; StartUpInfo: TStartUpInfo; &nbsp; // holds startup information<br>begin<br>&nbsp; {initialize the startup information}<br>&nbsp; FillChar(StartupInfo, SizeOf(TStartupInfo), 0);<br>&nbsp; with StartupInfo do<br><br>&nbsp; begin<br>&nbsp; &nbsp; cb := SizeOf(TStartupInfo);<br>&nbsp; &nbsp; dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; wShowWindow := SW_SHOWNORMAL;<br>&nbsp; end;<br><br>&nbsp; {launch a process}<br>&nbsp; CreateProcess('c:/Windows/calc.exe', nil, nil, nil, False,<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; ExitCode: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds the process exit code<br><br>begin<br>&nbsp; {terminate the process and retrieve the exit code}<br>&nbsp; TerminateProcess(ProcessInfo.HProcess, 10);<br>&nbsp; GetExitCodeProcess(ProcessInfo.HProcess, ExitCode);<br><br>&nbsp; {display the exit code}<br>&nbsp; Label1.Caption := 'The exit code is '+Inttostr(ExitCode);<br>end;<br>
 
WaitForSingleObject<br>WaitForSingleObjectEx
 
后退
顶部