I need this 'still_running' piece, any one have any ideas.(50分)

  • 主题发起人 主题发起人 lcmiaom
  • 开始时间 开始时间
L

lcmiaom

Unregistered / Unconfirmed
GUEST, unregistred user!
Q:<br>&nbsp; &nbsp;wProg := WinExec('thingy.exe', SW_SHOW)<br>&nbsp; &nbsp;while (still_running(wProg))<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessage;<br>&nbsp; &nbsp;...<br><br>I need this 'still_running' piece, any one have any ideas.
 
什么意思??
 
createprocess + waitforsingleobject
 
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, zAppName, nil, nil, false,<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then<br>&nbsp; &nbsp; &nbsp; Result := -1<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>&nbsp; &nbsp; end;<br>end;<br><br>[Error] PubUnit.pas(51): Types of actual and formal var parameters must be identical
 
How do I execute a program and have my code wait until it is finished?<br>From: Noel Rice &lt;nrice@ix.netcom.com&gt;<br>A: Here is the 16 bit version:<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>&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>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>32 bit version中:<br>GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>"[Error]: Types of actual and formal var parameters must be identical"<br>WHO CAN TELL ME WHY?
 
在Delphi中有一个做法是查找那个程序的主窗口名称,如果名称存在,表明其程序仍在<br>运行,应该可以解决这个问题
 
&nbsp; &nbsp;我运行的文件格式是*.bat,应该是找不到那个程序的主窗口名称的!
 
to lcmiaom:<br>人家可不是这个意思。<br>应该找窗口或其他标识程序的东西。
 
GetExitCodeProcess(ProcessInfo.hProcess,Result) 中:<br><br>1、 Result是delphi函数返回值用的,最好个名字<br>2、它的类型应该是DWORD,而不是integer
 
接受答案了.
 
受益韭浅!!
 
后退
顶部