Delphi 6 - 运行时读取控制台应用程序的输出

时间:2009-07-31 12:05:02

标签: delphi console

如何在运行时读取控制台应用输出。我启动控制台应用程序,并希望读取控制台应用程序打印的输出。

4 个答案:

答案 0 :(得分:13)

这个solution怎么样。


编辑:该链接指向此解决方案(为了清晰易读而删除使用with):

// The example runs 'chkdsk.exe c:\' and displays the output to Memo1.
// Put a TMemo (Memo1) and a TButton (Button1) on your form. Put this 
//   code in the OnCLick event procedure for Button1:

procedure TForm1.RunDosInMemo(DosApp:String;AMemo:TMemo) ;
const
  ReadBuffer = 2400;
var
  Security    : TSecurityAttributes;
  ReadPipe,
  WritePipe   : THandle;
  start       : TStartUpInfo;
  ProcessInfo : TProcessInformation;
  Buffer      : Pchar;
  BytesRead   : DWord;
  Apprunning  : DWord;

begin
  Security.nlength := SizeOf(TSecurityAttributes) ;
  Security.binherithandle := true;
  Security.lpsecuritydescriptor := nil;
  if Createpipe (ReadPipe, WritePipe, @Security, 0) then 
  begin
    Buffer := AllocMem(ReadBuffer + 1) ;
    FillChar(Start,Sizeof(Start),#0) ;
    start.cb := SizeOf(start) ;
    start.hStdOutput := WritePipe;
    start.hStdInput := ReadPipe;
    start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    start.wShowWindow := SW_HIDE;
    if CreateProcess(nil,
                     PChar(DosApp),
                     @Security,
                     @Security,
                     true,
                     NORMAL_PRIORITY_CLASS,
                     nil,
                     nil,
                     start,
                     ProcessInfo) then
    begin
      repeat
        Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
        Application.ProcessMessages;
      until (Apprunning <> WAIT_TIMEOUT) ;
      repeat
        BytesRead := 0;
        ReadFile(ReadPipe,Buffer[0],
        ReadBuffer,BytesRead,nil) ;
        Buffer[BytesRead]:= #0;
        OemToAnsi(Buffer,Buffer) ;
        AMemo.Text := AMemo.text + String(Buffer) ;
      until (BytesRead < ReadBuffer) ;
    end;
    FreeMem(Buffer) ;
    CloseHandle(ProcessInfo.hProcess) ;
    CloseHandle(ProcessInfo.hThread) ;
    CloseHandle(ReadPipe) ;
    CloseHandle(WritePipe) ;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
begin
  RunDosInMemo('chkdsk.exe c:\', Memo1) ;
end;

答案 1 :(得分:2)

我通常使用这个移植的FPC代码:http://www.stack.nl/~marcov/processdelphi.zip

它包含一个控制外部程序的类(它是Lazarus用来调用cmdline编译器和其他程序的类)。

文档在这里,但是delphi端口有点旧,因此上述版本中并不存在所有记录的属性。

http://www.freepascal.org/docs-html/fcl/process/index.html

答案 2 :(得分:1)

只是对Marco答案的一小部分补充,使用TProcess单元已经过彻底解释here

我相信这是最简单的方法。祝你好运!

答案 3 :(得分:0)

只是为了添加更多选项。

  1. 两篇基于 Windows API 构建此功能的文章:http://thundaxsoftware.blogspot.com/2012/12/capturing-console-output-with-delphi.html

  2. JediVCL 库(而且它很大,因此拥有和避免它都有优缺点)有一个 TJvCreateProcess 组件。
    https://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvCreateProcess
    在安装时:https://github.com/project-jedi/jvcl 可能还有 How to install JVCL packages if the installer failed when I ran it as a non-Administrator User

  3. JediVCL 安装程序使用具有简单界面的小型独立单元 CapExec.pashttps://github.com/project-jedi/jvcl/tree/master/jvcl/install/JVCLInstall/Helpers

type
  TCaptureLine = procedure(const Line: string; var Aborted: Boolean) of object;
.....
function CaptureExecute(const App, Args, Dir: string; CaptureLine: TCaptureLine;
  OnIdle: TNotifyEvent = nil;......): Integer;