delphi6,win2k:<br>procedure TForm1.btnRunClick(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> 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,'CMD.EXE /c dir/w');<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;