如何得到可执行程序的返回值?(100分)

  • 主题发起人 主题发起人 恒翔
  • 开始时间 开始时间

恒翔

Unregistered / Unconfirmed
GUEST, unregistred user!
这个程序需要调用perl写的程序,而perl的程序是在命令窗口运行的,我的程序是用来采集、分析数据的,分析的过程,显示了到达每一步的过程,如果单独执行这个Perl程序,就可以看到了,现在想把这个过程显示在Delphi写的街面上,各位老大,知道吗?
 
通过管道技术。可以的。
 
老大,具体点行吗,给个实例.
 
将DOS的标准输出转倒Memo中 <br>unit MainFrm;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls;<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;OpenDialog1: TOpenDialog;<br> &nbsp; &nbsp;btnRun: TButton;<br> &nbsp; &nbsp;btnOpenfile: TButton;<br> &nbsp; &nbsp;EditFilename: TEdit;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;procedure btnOpenfileClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnRunClick(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> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.btnOpenfileClick(Sender: TObject);<br>begin<br> if opendialog1.Execute then editfilename.Text :=opendialog1.FileName;<br>end;<br><br>procedure TForm1.btnRunClick(Sender: TObject);<br>var<br> hReadPipe,hWritePipe:THandle;<br> si:STARTUPINFO;<br> lsa:SECURITY_ATTRIBUTES;<br> pi:PROCESS_INFORMATION;<br> mDosScreen:String;<br> cchReadBuffer:DWORD;<br> ph:PChar;<br> fname:PChar;<br> i,j:integer;<br>begin<br> fname:=allocmem(255);<br> ph:=AllocMem(5000);<br> lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);<br> lsa.lpSecurityDescriptor :=nil;<br> lsa.bInheritHandle :=True;<br><br> if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then<br> begin<br> &nbsp; ShowMessage('Can not create pipe!');<br> &nbsp; exit;<br> end;<br> fillchar(si,sizeof(STARTUPINFO),0);<br> si.cb :=sizeof(STARTUPINFO);<br> si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);<br> si.wShowWindow :=SW_HIDE;<br> si.hStdOutput :=hWritePipe;<br> StrPCopy(fname,EditFilename.text);<br> if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False &nbsp;then<br> begin<br> &nbsp; ShowMessage('can not create process');<br> &nbsp; FreeMem(ph);<br> &nbsp; FreeMem(fname);<br> &nbsp; Exit;<br> end;<br><br> while(true) do<br> begin<br> &nbsp; if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;<br> &nbsp; if cchReadBuffer&lt;&gt;0 then<br> &nbsp; begin<br> &nbsp; &nbsp; if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;<br> &nbsp; &nbsp; ph[cchReadbuffer]:=chr(0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(ph);<br> &nbsp; end<br> &nbsp; else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;<br> &nbsp; Sleep(100);<br> end;<br><br> ph[cchReadBuffer]:=chr(0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add(ph);<br> CloseHandle(hReadPipe);<br> CloseHandle(pi.hThread);<br> CloseHandle(pi.hProcess);<br> CloseHandle(hWritePipe);<br> FreeMem(ph);<br> FreeMem(fname);<br>end;<br><br>end.<br>( 在命令行里打cmd/c dir看看:)
 
后退
顶部