启动两个进程并使用Delphi中的管道连接它们

时间:2010-06-02 12:09:13

标签: delphi pipe

我需要在程序中启动两个外部程序,并将第一个程序的STDOUT连接到第二个程序的STDIN。如何在Delphi中实现这一目标(RAD Studio 2009,如果重要的话)?我在Windows环境中运行。

作为命令行命令,我的情况看起来像这样:

dumpdata.exe | encrypt.exe "mydata.dat"

4 个答案:

答案 0 :(得分:9)

快速测试似乎有效(受JCL重点启发):

child1:说'你好,世界!' 3倍到标准输出

program child1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure Main;
var
  I: Integer;
begin
  for I := 0 to 2 do
    Writeln('Hello, world!');
  Write(^Z);
end;

begin
  try
    Main;
  except
    on E: Exception do
    begin
      ExitCode := 1;
      Writeln(ErrOutput, Format('[%s] %s', [E.ClassName, E.Message]));
    end;
  end;
end.

child2:回显OutputDebugString的标准输入(可以DebugView查看)

program child2;

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils, Classes;

procedure Main;
var
  S: string;
begin
  while not Eof(Input) do
  begin
    Readln(S);
    if S <> '' then
      OutputDebugString(PChar(S));
  end;
end;

begin
  try
    Main;
  except
    on E: Exception do
    begin
      ExitCode := 1;
      Writeln(ErrOutput, Format('[%s] %s', [E.ClassName, E.Message]));
    end;
  end;
end.

parent:启动child1重定向到child2

program parent;

{$APPTYPE CONSOLE}

uses
  Windows, Classes, SysUtils;

procedure ExecutePiped(const CommandLine1, CommandLine2: string);
var
  StartupInfo1, StartupInfo2: TStartupInfo;
  ProcessInfo1, ProcessInfo2: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeRead, PipeWrite: THandle;
begin
  PipeWrite := 0;
  PipeRead := 0;
  try
    SecurityAttr.nLength := SizeOf(SecurityAttr);
    SecurityAttr.lpSecurityDescriptor := nil;
    SecurityAttr.bInheritHandle := True;
    Win32Check(CreatePipe(PipeRead, PipeWrite, @SecurityAttr, 0));

    FillChar(StartupInfo1, SizeOf(TStartupInfo), 0);
    StartupInfo1.cb := SizeOf(TStartupInfo);
    StartupInfo1.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    StartupInfo1.wShowWindow := SW_HIDE;
    StartupInfo1.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
    StartupInfo1.hStdOutput := PipeWrite;
    StartupInfo1.hStdError := GetStdHandle(STD_ERROR_HANDLE);

    FillChar(StartupInfo2, SizeOf(TStartupInfo), 0);
    StartupInfo2.cb := SizeOf(TStartupInfo);
    StartupInfo2.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    StartupInfo2.wShowWindow := SW_HIDE;
    StartupInfo2.hStdInput := PipeRead;
    StartupInfo2.hStdOutput := GetStdHandle(STD_OUTPUT_HANDLE);
    StartupInfo2.hStdError := GetStdHandle(STD_ERROR_HANDLE);

    FillChar(ProcessInfo1, SizeOf(TProcessInformation), 0);
    FillChar(ProcessInfo2, SizeOf(TProcessInformation), 0);

    Win32Check(CreateProcess(nil, PChar(CommandLine2), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo2,
      ProcessInfo2));

    Win32Check(CreateProcess(nil, PChar(CommandLine1), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo1,
      ProcessInfo1));

    WaitForSingleObject(ProcessInfo2.hProcess, INFINITE);
  finally
    if PipeRead <> 0 then
      CloseHandle(PipeRead);
    if PipeWrite <> 0 then
      CloseHandle(PipeWrite);
    if ProcessInfo2.hThread <> 0 then
      CloseHandle(ProcessInfo2.hThread);
    if ProcessInfo2.hProcess <> 0 then
      CloseHandle(ProcessInfo2.hProcess);
    if ProcessInfo1.hThread <> 0 then
      CloseHandle(ProcessInfo1.hThread);
    if ProcessInfo1.hProcess <> 0 then
      CloseHandle(ProcessInfo1.hProcess);
  end;
end;

procedure Main;
begin
  ExecutePiped('child1.exe', 'child2.exe');
end;

begin
  try
    Main;
  except
    on E: Exception do
    begin
      ExitCode := 1;
      Writeln(Error, Format('[%s] %s', [E.ClassName, E.Message]));
    end;
  end;
end.

答案 1 :(得分:2)

CreateProcess()允许您重定向启动的应用程序的stdin和stdout。您的应用程序可以从第一个应用程序标准输出读取并写入第二个应用程序标准。

答案 2 :(得分:2)

以下是在Delphi XE中使用的更正代码。 CommandLine字符串必须是变量,并且还在ExecutePiped函数上方定义。

    program Parent;

    {$APPTYPE CONSOLE}

    uses
      Windows, SysUtils, Classes;

    var cmd1, cmd2 :string;

    function ExecutePiped(CommandLine1: string; CommandLine2: string):string;
    var
      StartupInfo1, StartupInfo2 : TStartupInfo;
      ProcessInfo1, ProcessInfo2 : TProcessInformation;
      SecurityAttr               : TSecurityAttributes;
      PipeRead, PipeWrite        : THandle;
      Handle                     : Boolean;
      WorkDir                    : String;
    begin
      PipeWrite := 0;
      PipeRead  := 0;
      try
        SecurityAttr.nLength              := SizeOf(SecurityAttr);
        SecurityAttr.bInheritHandle       := True;
        SecurityAttr.lpSecurityDescriptor := nil;

        CreatePipe(PipeRead, PipeWrite, @SecurityAttr, 0);

        FillChar(StartupInfo1, SizeOf(TStartupInfo), 0);
        StartupInfo1.cb          := SizeOf(TStartupInfo);
        StartupInfo1.dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        StartupInfo1.wShowWindow := SW_HIDE;
        StartupInfo1.hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
        StartupInfo1.hStdOutput  := PipeWrite;
        StartupInfo1.hStdError   := GetStdHandle(STD_ERROR_HANDLE);

        FillChar(StartupInfo2, SizeOf(TStartupInfo), 0);
        StartupInfo2.cb          := SizeOf(TStartupInfo);
        StartupInfo2.dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        StartupInfo2.wShowWindow := SW_HIDE;
        StartupInfo2.hStdInput   := PipeRead;
        StartupInfo2.hStdOutput  := GetStdHandle(STD_OUTPUT_HANDLE);
        StartupInfo2.hStdError   := GetStdHandle(STD_ERROR_HANDLE);

        FillChar(ProcessInfo1, SizeOf(TProcessInformation), 0);
        FillChar(ProcessInfo2, SizeOf(TProcessInformation), 0);

        WorkDir := '';

        Handle := CreateProcess(nil, PChar(CommandLine2), nil, nil, True, 0, nil, PChar(WorkDir), StartupInfo2, ProcessInfo2);
        Handle := CreateProcess(nil, PChar(CommandLine1), nil, nil, True, 0, nil, PChar(WorkDir), StartupInfo1, ProcessInfo1);

        WaitForSingleObject(ProcessInfo2.hProcess, INFINITE);

      finally

        if PipeRead              <> 0 then CloseHandle(PipeRead);
        if PipeWrite             <> 0 then CloseHandle(PipeWrite);

        if ProcessInfo2.hThread  <> 0 then CloseHandle(ProcessInfo2.hThread);
        if ProcessInfo2.hProcess <> 0 then CloseHandle(ProcessInfo2.hProcess);

        if ProcessInfo1.hThread  <> 0 then CloseHandle(ProcessInfo1.hThread);
        if ProcessInfo1.hProcess <> 0 then CloseHandle(ProcessInfo1.hProcess);

      end;

    end;

    procedure Main;
    begin
      cmd1 := '"child1.exe"';
      cmd2 := '"child2.exe"';
      ExecutePiped(cmd1, cmd2);
    end;

    begin
      try
        Main;
      except
        on E: Exception do
        begin
          ExitCode := 1;
          Writeln(Error, Format('[%s] %s', [E.ClassName, E.Message]));
        end;
      end;
    end.

测试我已修改Child2.pas将接收到的文本写入文件。

    program Child2;

    {$APPTYPE CONSOLE}

    uses
    Windows, SysUtils, Classes;

    procedure Main;
    var S: string;
        OutFile : TextFile;
    begin
      AssignFile(OutFile, 'test.txt');
      Rewrite(OutFile);
      while not Eof(Input) do
      begin
        Readln(S);
        Writeln(OutFile,S);
        //if S <> '' then OutputDebugString(PChar(S));
      end;
      CloseFile(OutFile);
    end;

    begin
      try
        Main;
      except
        on E: Exception do
        begin
          ExitCode := 1;
          Writeln(ErrOutput, Format('[%s] %s', [E.ClassName, E.Message]));
        end;
      end;
    end.

答案 3 :(得分:0)

这种方法应该有效。在担心从Delphi调用它之前,通过在命令提示符窗口(DOS窗口)中运行来获得命令行 然后只需使用WinExec或ShellExecute从Delphi调用该命令。有呼叫和等待的选项,或只是“发射并忘记”。