[fpc-pascal] TProcess on windows: incompatibility between poUsePipes and InheritHandles:=false
Luca Olivetti
luca at ventoso.org
Tue Mar 11 17:18:12 CET 2014
In order to use poUsePipes, InheritHandles must be true
(see
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331%28v=vs.85%29.aspx)
This is a limitation of the windows api, but I wonder if is there a
workaround to avoid inheriting other handles.
Long story:
I need to spawn a program (relatively short lived) and capture its
standard output/standard error.
The main program has various files open as well as some connected
sockets (using synapse as the socket library).
In a thread I have a
while not terminated do
begin
....
LocBarcode:=FChannel.RecvTerminated(-1,cr);
....
end;
(where FChannel is a TTcpBlockSocket, a synapse socket) and in the
thread destructor I have
Terminate;
FChannel.AbortSocket;
inherited Destroy;
so that the above RecvTerminated will exit with an error and the thread
terminates cleanly.
The problem is that if the child process is still running, that doesn't
work anymore, until the process terminates. That's surely because the
process has inherited the socket, so I tried InheritHandles:=false, but
I discovered that it precludes the use of poUsePipes.
My current workaround is to terminate the process before trying to
destroy the thread, and that works, but I wonder if having unneeded
handles in a child process could have other unintended consequences, so
I'd like to know if is there a better workaround that allows to capture
stdout/stderr while at the same time avoiding inheriting other handles.
Possibly in a "generic" way, so that I don't need to have knowledge of
which handles are currently opened.
Bye
--
Luca
More information about the fpc-pascal
mailing list