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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With