[fpc-pascal] Redirecting input to a child process
Michael Van Canneyt
michael at freepascal.org
Thu May 19 00:15:17 CEST 2011
On Thu, 19 May 2011, Anton Shepelev wrote:
> Ludo Brands:
>
>> You can file a bug at http://bugs.freepascal.org/.
>
> This is bug #0019325.
>
> ...
> I thought about writing a patch, but it seems that it is not
> enough to modify the implementation of the Windows-specific
> pipes.inc.
>
> The correct way to create pipes for the three channels (out,
> in and err) on Windows is to create non-inheritable handles
> and duplicate only the child process's ends of them into
> inheritable ones, which means that the CreatePipeHandles
> function must accept a third parameter indicating which end
> should be inherited: for OUT and ERR channels it will be the
> write-end and for IN -- the read-end.
>
> Here is a possible implementation:
>
> {--------------- changes to pipes.inc -----------------}
> Interface
> {...}
> type
> TDupOpt = (dupIn, dupOut);
> TDupOpts = Set of TDupOpt;
>
> Implemenation
> {...}
> function DuplicateHandleFP(var handle: THandle): Boolean;
> var
> oldHandle: THandle;
> begin
> oldHandle := handle;
> Result := DuplicateHandle
> ( GetCurrentProcess(),
> oldHandle,
> GetCurrentProcess(),
> @handle,
> 0,
> true,
> DUPLICATE_SAME_ACCESS
> );
> if Result then
> Result := CloseHandle(oldHandle);
> end;
>
> Function CreatePipeHandles
> (Var Inhandle,OutHandle : THandle; DupOpts: TDupOpts) : Boolean;
> begin
> Result := CreatePipe (Inhandle,OutHandle, at piNonInheritablePipe,0);
> if Result and (dupIn in DupOpts) then
> Result := DuplicateHandleFP(Inhandle);
> if Result and (dupOut in DupOpts) then
> Result := DuplicateHandleFP(OutHandle);
> end;
>
> {----------- process.inc -------------------------}
> {CreatePipes will have these calls:}
> begin
> CreatePipeHandles(SI.hStdInput ,HI, [dupIn ]);
> CreatePipeHandles(HO,Si.hStdOutput, [dupOut]);
> if CE then
> CreatePipeHandles(HE,SI.hStdError, [dupOut])
> {...}
>
> Unfortunately, the third parameter will have to be added to
> this function in all other platform-specific pipes.inc
> files, but these will just ignore it and work as before.
>
> If it is preferable to keep the interface of the pipes unit
> unchanged, the windows-specific part could just be chaged to
> create non-inheritable pipes, and the duplication could be
> moved into the windows-specific process.inc.
>
> Will you accept one of such patches?
Yes.
The interface of the pipes unit can be changed with a parameter with
a default value, so existing code continues to work.
Michael.
More information about the fpc-pascal
mailing list