WAITAPI请教(50分)

  • 主题发起人 主题发起人 ywws
  • 开始时间 开始时间
Y

ywws

Unregistered / Unconfirmed
GUEST, unregistred user!
我用如下函数在DELPHI中调用一DOS程序并等待它结束,可是当此程序<br>运行完毕后却一定让我关闭DOS窗口,才执行下一语句,我想不显示此<br>窗口,却DEADLOCK了。该如何让它执行完后自动执行下一语句呢,当然<br>若没执行完,超过TIMEOUT后也自动执行下一语句。<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>&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>用winexec就可以了.<br><br>var pWindowsList: pointer;<br>&nbsp; &nbsp; hActiveWindow: HWnd;<br>&nbsp; &nbsp; hExeHandle: THandle;<br>begin<br>pWindowsList := DisableTaskWindows(0);<br>hActiveWindow := GetActiveWindow;<br>try<br>&nbsp; hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);<br>&nbsp; while GetModuleUsage(hExeHandle) 0 do<br>&nbsp; Application.ProcessMessages;<br>finally<br>&nbsp; EnableTaskWindows(pWindowsList);<br>&nbsp; SetActiveWindow(hActiveWindow);<br>end;<br>end;<br><br>
 
太慢了,贴了半天才贴上.擦擦汗.....
 
to www:<br>&nbsp; 我查不到GETMODULEUSAGE函数,能不能重写一下这一行<br>while GetModuleUsage(hExeHandle) 0 do
 
GetModuleUsage是win3.x的16位函数,在win9.x中已经被删除了。除非你用<br>delphi1.0写这个程序。
 
&nbsp;有没有人知道啊,为什么要关了DOS窗口才算程序运行结束呢?
 
试试 CreateProcess吧。
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>Function MyPing(const Host:string):boolean;<br>var<br>&nbsp; CmdLinePChar:array[0..120] of char;<br>&nbsp; StartUpInfo:TStartUpInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>&nbsp; HOutput:THandle;<br>&nbsp; StringList:TStringList;<br>&nbsp; TempFileName:String;<br>&nbsp; i:integer;<br>begin<br>&nbsp; Result:=false;<br>&nbsp; Screen.Cursor:=crHourGlass;<br>&nbsp; StringList:=TStringList.Create;<br>&nbsp; try<br>&nbsp; &nbsp; TempFileName:=ExtractFilePath(application.ExeName)+'tempfile.tmp';<br>&nbsp; &nbsp; HOutput:=FileCreate(TempFileName);<br>&nbsp; &nbsp; if HOutput&lt;0 then<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; StrPCopy(CmdLinePChar,'Ping.exe'+Host);<br>&nbsp; &nbsp; FillChar(StartUpInfo,sizeof(StartUpInfo),#0);<br>&nbsp; &nbsp; with StartUpInfo do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cb:=sizeof(StartUpInfo);<br>&nbsp; &nbsp; &nbsp; dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;<br>&nbsp; &nbsp; &nbsp; wShowWindow:=SW_Show;<br>&nbsp; &nbsp; &nbsp; hstdOutput:=HOutput;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if CreateProcess(nil,CmdLinePChar,nil,nil,True,0,nil,nil,StartUpInfo,ProcessInfo) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; WaitForSingleObject(Processinfo.hProcess,INFINITE);<br>&nbsp; &nbsp; &nbsp; FileClose(HOutput);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; FileClose(HOutput);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; StringList.LoadFromFile(TempFileName);<br>&nbsp; DeleteFile(TempFileName);<br>&nbsp; for i:=1 to StringList.Count-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; if pos('Reply from',StringList)&gt;=1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result:=true;<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; finally<br>&nbsp; screen.Cursor:=crDefault;<br>&nbsp; form1.edit1.text:=stringlist;<br>&nbsp; StringList.Free;<br><br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>MyPing(' 172.18.128.182');<br>end;<br><br>end.
 
&lt;b&gt;shellexecute&lt;/b&gt; needn't wait for finish.
 
多人接受答案了。
 

Similar threads

W
回复
5
查看
213
weisunding
W
回复
20
查看
367
龙石佛
Z
回复
5
查看
208
zhenghui
Z
后退
顶部