August 27th, 2003, 08:23 AM
stdin redirection doesn't seem to work
Help on this would be really appreciated. I have adapted code examples found in MSVC++ to redirect stdin (example given was for stdout which worked OK). Idea is that the parent writes to a pipe and the child receives through stdin.
I use the following to create the pipe and duplicate stdin etc ...
if(_pipe(hStdInPipe, PIPE_SIZE, O_BINARY | O_NOINHERIT) == -1)
hStdIn = _dup(_fileno(stdin));
if(_dup2(hStdInPipe[READ_HANDLE], _fileno(stdin)) != 0)
Then I spawn the process and as I understand it the child will now use the read channel of the pipe when referencing stdin. Next I retrieve stdin for further use in the parent using ...
if(_dup2(hStdIn, _fileno(stdin)) != 0)
And then after checking I have a handle to the process I write to the pipe ...
write( hStdInPipe[WRITE_HANDLE], myDataPtr, sizeof(myDataPtr))
The result is that the child is spawned OK but hangs as soon as I try to read from stdin using ...
fgets(myData, nLength+1, stdin);
Am I making some obvious mistake with the pipe or dup calls or even trying to use the wrong ends of the pipes? I have successfully managed to get a childs stdout piped back to the parent and figured this should be just as easy.
Also are there any issues with using pipes on certain flavours of Windows? I've seen several references about this but none answered too clearly.