[fpc-devel] TProcess and redirection of StdIn/Out (e.g. from/to files)

Michael Van Canneyt michael at freepascal.org
Thu Dec 14 23:08:38 CET 2023



On Thu, 14 Dec 2023, Martin Frb via fpc-devel wrote:

> On 14/12/2023 21:33, Marco van de Voort via fpc-devel wrote:
>>
>> Op 14-12-2023 om 21:27 schreef Martin Frb via fpc-devel:
>>>
>>> I am actually pretty sure, on Linux, I can get what I want by doing 
>>> it in the "OnFork" event of TProcess.
>>> But on Windows it is well hidden away in the "Execute" method, 
>>> nothing virtual that could be intercepted.
>>
>> Change the input handle and use popassinput ?
> As I said, not possible (unless I make a copy of the entire TProcess 
> code).  There is no way to intercept "Execute", and it must be done 
> before the Windows API is called to create the process.
>
>>
>> But I wonder if this is not too specialistic. It depends on how well 
>> you can abstract it into TProcess, and preferably in a somewhat 
>> similar way for all OSes.
>
> Well Michael's answer looks like he is about to have what I need. 
> Depends on the when...

I pushed what I have to the extended_process branch. I tested on linux.
Most simple scenarios I threw at it seem to work OK but I need to do some more testing
for correct pipelines, for example:

ProcessA | ProcessB > out.txt

(so yes, we should be able to make a good 'pascal shell' :-))

Windows compiles and in code does what it takes, but needs more testing.

(in particular: when opening a file for input/output, does the parent need to close the file
just as it does for pipes ?)

Note that there are still writeln() stements in it, this is WIP.

You're  of course welcome to provide patches if I messed something up :)

Michael.


More information about the fpc-devel mailing list