Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


Pipe as Stdin and Stdout

OSR_Community_UserOSR_Community_User Member Posts: 110,217
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

Comments

  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    Pipe as Stdin and Stdout>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.

    Try using CRT functions like pipe() etc instead of Win32 calls - this worked
    OK for me.

    Max
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 30 January 2023 Live, Online
Developing Minifilters 20 March 2023 Live, Online
Writing WDF Drivers TBD 2023 Live, Online
Internals & Software Drivers 17 April 2023 Live, Online