Hi,
I am using pipes for redirecting the Stdin and Stdout of a process.
For this I am creating two pipes : one for Stdin and other for Stdout as
follows:
// Create pipe for passing commands to application
Stdin = CreateNamedPipe(
“\\.\pipe\pipe_Input”,
PIPE_ACCESS_OUTBOUND
| FILE_FLAG_WRITE_THROUGH,
PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,
1,//Instances
1,// Input buffer
1,// Output buffer
0,//Timeout
NULL);
if( Stdin == INVALID_HANDLE_VALUE)
printf(“\n Creation of Stdin pipe failed”);
// Create pipe for reading output of application
Stdout = CreateNamedPipe(
“\\.\pipe\pipe_Output”,
PIPE_ACCESS_INBOUND
| FILE_FLAG_WRITE_THROUGH,
PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,
1, // Instances
1,// Input buffer
1,// Output buffer
0,//Timeout
NULL);
if( Stdout == INVALID_HANDLE_VALUE)
printf(“\n Creation of Stdout pipe failed”);
Then I open these pipes to obtain client side handles to these pipes.
InputPipeHandle = CreateFile(
“\\.\pipe\pipe_Input”,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&SecurityAttributes,// Inherit flag set to
true
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
NULL);
if( InputPipeHandle == INVALID_HANDLE_VALUE)
printf(“\n Creation of InputPipeHandle pipe failed”);
OutputPipeHandle = CreateFile(
“\\.\pipe\pipe_Output”,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&SecurityAttributes, // Inherit flag set to true
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
NULL);
if( OutputPipeHandle == INVALID_HANDLE_VALUE)
printf(“\n Creation of OutputPipeHandle pipe failed”);
Then I am setting STARTUPINFO structure as follows to set the above handles
as Stdin and Stdout:
ZeroMemory( &StartupInfo, sizeof(StartupInfo) );
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
StartupInfo.hStdError = OutputPipeHandle;
StartupInfo.hStdInput = InputPipeHandle;
StartupInfo.hStdOutput = OutputPipeHandle;
And then I call the CreateProcess() function.
The problem comes while getting output from the new process and Sending
input to the new process. The problem is that the parent process is not able
to read until the pipe buffer is full or the child process exits in which
case buffers are flushed. Same problem is with input pipe buffer. In other
words if the new process prints the outputs and EXITS then I am able to read
the output from pipe but If i want to run New process as INTERACTIVE app
then I am not able to give it input or read the output.
Note that I have specified the size of the buffer as 1 byte and also I
specified the pipe as BYTE and read mode as BYTE_MODE. I tried the MESSAGE
mode also but without success.
Also if I use fflush(stdout) in my new process app then I am able to read
from the pipe.
Can anybody indicate the problem and how to solve it.
Thanks
Ashish