请问如何控制在程序中调用的外部进程?(50分)

  • 主题发起人 主题发起人 番番
  • 开始时间 开始时间

番番

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序一开始就调用一个外部程序,想要该程序执行完毕后再回到当前程序中来.<br>请教如何知道调用的外部程序已经执行完毕.我用ENUMWINDOWS,可是编译提示需要<br>参数,可我已经给参数了.HELP ME!<br>
 
用WaitForSingleObject(...)<br>把你建的外部程序的handle作为参数,在你的程序中用WaitForSingleObject(...)等待INFINITE,<br>直到等待的外部程序完成。<br><br>WaitForSingleObject的用法非常简单,help文件里有详细的说明
 
以前有人回答过:copy to you<br><br>前几天看uddf的时候,发现了如下的源程序:<br>From: Noel Rice <br><br>A: Here is the 16 bit version:<br><br><br>--------------------------------------------------------------------------------<br><br>uses Wintypes,WinProcs,Toolhelp,Classes,Forms;<br><br>Function WinExecAndWait(Path : string; Visibility : word) : word;<br>var<br>&nbsp; InstanceID : THandle;<br>&nbsp; PathLen : integer;<br>begin<br>&nbsp; { inplace conversion of a String to a PChar }<br>&nbsp; PathLen := Length(Path);<br>&nbsp; Move(Path[1],Path[0],PathLen);<br>&nbsp; Path[PathLen] := #00;<br>&nbsp; { Try to run the application }<br>&nbsp; InstanceID := WinExec(@Path,Visibility);<br>&nbsp; if InstanceID &lt; 32 then { a value less than 32 indicates an Exec error }<br>&nbsp; &nbsp; &nbsp;WinExecAndWait := InstanceID<br><br>&nbsp; else begin<br>&nbsp; &nbsp; Repeat<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; until Application.Terminated or (GetModuleUsage(InstanceID) = 0);<br>&nbsp; &nbsp; WinExecAndWait := 32;<br>&nbsp; end;<br>end;<br><br>--------------------------------------------------------------------------------<br>Here is the 32 bit version:<br><br><br>--------------------------------------------------------------------------------<br><br>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><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><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>
 
谢谢你,可我怎样才能取得外部程序的handle呢?
 
ok,thanks,我试一下,成功立马给分.
 
to:cnaoszh <br>GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>在编译时出错.<br>[Error] Main.pas(392): Types of actual and formal var parameters must be identical<br>这怎么办啊?
 
var<br>ExitCode:DWORD;<br>begin<br>.....<br>GetExitCodeProcess(ProcessInfo.hProcess,ExitCode);<br><br>end;<br><br>
 
多人接受答案了。
 
可是该函数运行后就不执行下面的程序了.
 
执行,这是因为你的应用程序没有结束,用个可见的程序测试例如word退出后,程序继续执行<br>
 
WinExecAndWait32(FileName:String; Visibility : integer)<br>Visibility 值为多少?<br>我调用的的程序要带参数运行.用SHELLEXECUTE可以吗?
 
Visibility应该是:<br><br>Value Meaning<br>SW_HIDE Hides the window and activates another window.<br>SW_MAXIMIZE Maximizes the specified window.<br>SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.<br>SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.<br>SW_SHOW Activates the window and displays it in its current size and position. <br>SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. <br>SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.<br>SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.<br>SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.<br>SW_SHOWNA Displays the window in its current state. The active window remains active.<br>SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.<br>SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.<br>
 
不带参数可以运行,可是带参数怎么办,该函数好像没有执行文件的参数设置.
 
应该是这样的<br>function WinExecAndWait32(FileName:String; Visibility : integer):dword;<br>{返回值是dword型}<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><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 := 0 { pointer to PROCESS_INF }<br><br>&nbsp; else begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>&nbsp; end;<br>end;<br>
 
后退
顶部