I'm using CreateProcess
to launch an interactive script interpreter and would like to transparently forward stdin/stdout/stderr from/to the interpreter.
My first attempt was to setup the STARTUPINFO
structure passed to CreateProcess
like
STARTUPINFOA si = { sizeof( si ) };
si.hStdError = ::GetStdHandle( STD_ERROR_HANDLE );
si.hStdOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
si.hStdInput = ::GetStdHandle( STD_INPUT_HANDLE );
si.dwFlags |= STARTF_USESTDHANDLES;
I.e. I tried to make the script interpreter process use the very same handle for reading/writing as my launcher process uses. That didn't seem to work though (I'm not even sure those standard handles can be inherited).
A second idea, based on the Creating a Child Process with Redirected Input and Output example is to setup three pipes to forward all data written to any of the pipes. Since I don't know how to wait for data to be written to more than one file (WaitForMultipleObjects
cannot synchronize on pipes), I was considering to have three threads, each of which doing a blocking ReadFile
call on a pipe.
I suspect that this might be overkill though so I'm wondering: is there some easier way to do this? I don't need to do any kind of processing of the data passed from/to the script interpreter at all.
As a side note, on Linux I'm using execvp
to just replace the current process with the script interpreter process, but on Windows I need to launch the script interpreter with the main thread in suspended state (so that I can do some bytecode patching) - so even since _execvp seems to be availble on Windows, I apparantly have to use CreateProcess.