两个程序之间的管道Delphi

时间:2017-12-27 05:40:43

标签: delphi pipe piping

我想制作一个能在两个程序之间进行管道的程序

从第一个获取输入,将其输入第二个将对其进行处理并将其返还给我,我将首先回馈

  if Input <> '-' then
    InS := TFileStream.Create(Input, fmOpenRead or fmShareDenyWrite)
  else
    InS := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));

  if Output <> '-' then
    OutS := TFileStream.Create(Output, fmCreate or fmShareExclusive)
  else
    OutS := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));

  FillChar(StartupInfo, sizeof(StartupInfo), 0);
  FillChar(ProcessInfo, sizeof(ProcessInfo), 0);

  SecurityAttributes.nLength := sizeof(SecurityAttributes);
  SecurityAttributes.bInheritHandle := True;
  SecurityAttributes.lpSecurityDescriptor := Nil;

  CreatePipe(OutPipe, InPipe, @SecurityAttributes, 0);

  StartupInfo.cb := sizeof(StartupInfo);
  StartupInfo.wShowWindow := SW_HIDE;
  StartupInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
  StartupInfo.hStdInput := InPipe;
  StartupInfo.hStdOutput := OutPipe;

  Handle := CreateProcess(PChar(CLSAppInfo.CLSExeName),
    PChar(Format('a - -', [])), nil, nil, True, 0, nil, PChar(GetCurrentDir),
    StartupInfo, ProcessInfo);

  if Handle then
  begin
    GetMem(Buffer, BLOCK_SIZE);
    repeat
      ReadByte := InS.Read(Buffer^, BLOCK_SIZE);
      isOK := WriteFile(InPipe, Buffer^, ReadByte, WroteByte, nil);

      WriteLn(ReadByte.ToString);
      WriteLn(isOK.ToString());

      if (not isOK) or (ReadByte = 0) then
        break;

      repeat
        isOK := ReadFile(OutPipe, Buffer^, 255, ReadByte, nil);
        if not isOK then
          break;
        if ReadByte = 0 then
          break;

        OutS.Write(Buffer, ReadByte);
      until 0 = 1;
    until 0 = 1;

    FreeMem(Buffer);
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);
  end;

  CloseHandle(InPipe);
  CloseHandle(OutPipe);

  OutS.Free;
  InS.Free;

但这只是启动程序,程序结束时没有做任何事情

0 个答案:

没有答案