• Advertisement
Sign in to follow this  

Accessing STDOUT of a child process

This topic is 1433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I am trying to access the STDOUT of a child console application,
At first I tried to use Anonymous Pipes with Info from:

http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx
http://www.codeproject.com/Articles/5531/Redirecting-an-arbitrary-Console-s-Input-Output

then, I found this page at Microsoft Support : http://support.microsoft.com/kb/190351 that states:

"Note Child processes that use such C run-time functions as printf() and fprintf() can behave poorly when redirected. The C run-time functions maintain separate IO buffers. When redirected, these buffers might not be flushed immediately after each IO call. As a result, the output to the redirection pipe of a printf() call or the input from a getch() call is not flushed immediately and delays, sometimes-infinite delays occur. This problem is avoided if the child process flushes the IO buffers after each call to a C run-time IO function. Only the child process can flush its C run-time IO buffers. A process can flush its C run-time IO buffers by calling the fflush() function."

Any Idea what can I do in order to  solve this besides modifying the source?
 

Edited by Eliad Moshe

Share this post


Link to post
Share on other sites
Advertisement

Hi guys,
I am trying to access the STDOUT of a console application,
At first I tried to use Anonymous Pipes with Info from:
...
Any Idea what can I do in order to  solve this besides modifying the source?

It is sometimes sad how Windows has sacrificed simplicity for security.

In the Unix world, the command is popen(). Give it a program to run, and the stdin and stdio come back to you as piped streams for reading and writing.


The first link you gave is the Windows way of doing it. Just pass a NULL security token to create the pipes, then create the process with a default security token,using the pipes you made.

Share this post


Link to post
Share on other sites

Thanks frob for your help,

Should it works well with function calls like printf() at the called child process?
I used this source as a reference and the Read function from the calling process is blocking 'infinitely'..

 

Share this post


Link to post
Share on other sites
Probably.

In the Unix world people are very careful about getting data from stdin and stdout, building tools that are lightweight and are designed to be invoked by other programs.

In the Windows world, everything is designed around the screen. Many programs unintentionally (or intentionally) will not work when invoked by other programs.

If the program is getting input from the stdin stream and writing to stdout, then yes, everything should work. You need to feed it all the data that would normally be typed by the user and you need to read the values that would show up on the screen.

If the program tries to get fancy with non-stream console I/O or creating its own text console or doing other stuff that is increasingly common in the Windows world, then all bets are off.

Share this post


Link to post
Share on other sites

If the program tries to get fancy with non-stream console I/O or creating its own text console or doing other stuff that is increasingly common in the Windows world, then all bets are off.

Yep //

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement