Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateProcess and capture stdout

Tags:

c

windows

winapi

When using CreateProcess to run another program, what is the recommended way to capture the stdout? That is, to take whatever the second program was printing to stdout, and end up with it in an array where the first program can analyze it?

Both programs are straight Win32 programs written in C, no fancy stuff.

like image 517
rwallace Avatar asked Aug 24 '26 01:08

rwallace


1 Answers

The short answer is to create an anonymous pipe, setting the hStdOut/hStdErr and dwFlag members of the STARTUPINFO structure accordingly, and have CreateProcess() inherit the handle for the writing end of the pipe. Don't forget to close your writing handle of your pipe, then you can read from the reading handle of the pipe in a loop until it fails with an ERROR_BROKEN_PIPE error.

MSDN provides a detailed example of this:

Creating a Child Process with Redirected Input and Output

You are not the first person to do this, there should be plenty of example code and duplicate questions here on StackOverflow.

like image 80
Anders Avatar answered Aug 26 '26 15:08

Anders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!