[fpc-pascal] Redirecting input to a child process
Anton Shepelev
anton.txt at gmail.com
Wed May 18 23:10:07 CEST 2011
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?
Anton
More information about the fpc-pascal
mailing list