>“用管道不行。DOS程序不是我写的”<br>管道和DOS程序是不是你写的有什么关系?<br><br>下面是运行Dos命令nbtstat -a 192.168.0.15,并得到输出的例子:<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Memo1: TMemo;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> hReadPipe,hWritePipe:THandle;<br> si:STARTUPINFO;<br> lsa:SECURITY_ATTRIBUTES;<br> pi
ROCESS_INFORMATION;<br> mDosScreen:String;<br> cchReadBuffer
WORD;<br> ph
Char;<br> fname
Char;<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> ShowMessage('Can not create pipe!');<br> exit;<br> end;<br><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,'nbtstat -a 192.168.0.15');<br><br> if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then<br> begin<br> ShowMessage('can not create process');<br> FreeMem(ph);<br> FreeMem(fname);<br> Exit;<br> end;<br><br> while(true) do<br> begin<br> if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;<br> if cchReadBuffer<>0 then<br> begin<br> if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;<br> ph[cchReadbuffer]:=chr(0);<br> Memo1.Lines.Add(ph);<br> end<br> else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;<br> Sleep(100);<br> end;<br><br> ph[cchReadBuffer]:=chr(0);<br> 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>