* * *

Author Topic: TProcess with pipes to subprocess - not stdin/stdout  (Read 842 times)

nagle

  • Newbie
  • Posts: 4
TProcess with pipes to subprocess - not stdin/stdout
« on: February 11, 2017, 07:03:23 am »
I'm porting something, and I want to run a non-Free Pascal subprocess from a parent Free Pascal process. The parent needs pipes to and from the subprocess. TProcess supports this, but only if the I/O is via the subprocess's stdin and stdout. I want to create pipes in the parent process in Free Pascal, and pass them to the subprocess.  (Stdout in the subprocess is being used for human-readable output, and unavailable for the structured data being passed in and out.) Is there any way to do this?

In the original program, the parent process created pipes and sent the FD numbers to the child process on the command line. The child process then connected them up.  All this requires is that TProcess not close some specific file descriptors when forking. But there's no obvious option for that.

Thaddy

  • Hero Member
  • *****
  • Posts: 4025
Re: TProcess with pipes to subprocess - not stdin/stdout
« Reply #1 on: February 11, 2017, 10:09:21 am »
If you still need stdin and stdout in a different context from the child process, I would suggest to derive a new class from TProcess with a SecondaryInput and SecondaryOutput. (Mostly copy/paste code and a few extra fields)

If that is not necessary you can simply redirect StdIn, StdOut and (mis?-)use StdErr for console output.
« Last Edit: February 11, 2017, 10:11:13 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5537
Re: TProcess with pipes to subprocess - not stdin/stdout
« Reply #2 on: February 11, 2017, 03:10:35 pm »
Passing fd's on commandline is not portable, and tprocess is a portable component.

Probably it is better to take the *nix part of tprocess and start modifying that for your specific needs.

derek.john.evans

  • Guest
Re: TProcess with pipes to subprocess - not stdin/stdout
« Reply #3 on: February 11, 2017, 03:47:28 pm »
In the unit Pipes, there is:
Code: Pascal  [Select]
  1. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  2.                              Var OutPipe : TOutputPipeStream);
  3.  

Could you create your pipes using this, then use TProcess only to execute the subprocess? Assuming the Handles for TInputPipeStream/TOutputPipeStream are compatible.


 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus