高难度动作:谁能给出把DOS窗口下交互式程序的标准输出通过管道放到标准的Windows控件中...我在Delphi下用CreatePipe与CreateProcess实现了把DOS程序(准确的说是控制台程序)的标准输出通过管道放到一个Memo控件中,即:比如执行ping 127.0.0.1这个命令,输出结果可以在Memo中显示,且与Dos状态下一样。
但是程序只能执行如ping ,netstat这样的程序,不能操作FTP等这样的交互式的程序,不知道该如何完成?希望哪位高手、大虾能给出程序的思想或者代码,值此佳节,高分回溃!!
但是程序只能执行如ping ,netstat这样的程序,不能操作FTP等这样的交互式的程序,不知道该如何完成?希望哪位高手、大虾能给出程序的思想或者代码,值此佳节,高分回溃!!
begin
saAttr.nLength := sizeof(TSECURITYATTRIBUTES);
saAttr.bInheritHandle := true;
saAttr.lpSecurityDescriptor := nil; if not CreatePipe(hRead, hWrite,@saAttr,0) then
begin
Form1.StatusBar1.SimpleText:='创建输出管道失败!';
Exit;
end; try
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE and SW_SHOWMINNOACTIVE;
StartupInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
StartupInfo.hStdOutput:= hWrite; //we catch output
StartupInfo.hStdError := hWrite; //and also error if not CreateProcess(nil,FName, nil, nil, true, CREATE_NEW_CONSOLE, nil, nil, StartupInfo, ProcessInfo) then
begin
Form1.StatusBar1.SimpleText:='执行失败:请输入程序名!例如ping ,c:\XXX.exe';
exit;
end
else
while WaitforSingleObject(ProcessInfo.hProcess,0) <> WAIT_OBJECT_0 do ; //把执行输出的东东放到一个流中
OutSt := TMemoryStream.Create;
repeat
if ReadFile(hRead, Buffer, 80, bRead, nil) then
OutSt.WriteBuffer(Buffer, bRead)
else
break;
until bRead <> 80; OutSt.Seek(0,0); //移动流开始位置
OutPut.LoadFromStream(OutSt);
OutSt.Free; //释放空间
finally
//关句柄
CloseHandle(hRead);
CloseHandle(hWrite);
if not FreeConsole then Form1.StatusBar1.SimpleText:='成功执行 !';
end;
Borland\CBuilder5\Examples\WinTools
这个例子是用CB开发一个GUI,包装了CB附带的各种命令行工具.包括BC编译器