In my console app I've created a process by using CreateProcess method. Now, this process either writes to the console "OK" or "Not OK". How can I intercept that information so I can as well as to console write it to a file?
-
read the documentation of `CreateProcess` again and pay attention to the `STARTUPINFO` struct – David Haim Dec 13 '16 at 14:11
-
Reopened the question because the proposed duplicate deals with the general case of getting the output, while this question is about a special case that doesn't even need output, and where the proposed duplicate's answers would be overkill, far too complex. – Cheers and hth. - Alf Dec 13 '16 at 14:30
2 Answers
- Use
CreatePipe
to create an anonymous pipe. - Set the standard output of your child to be the write end of the pipe
- Parent process reads from the read end of the pipe
- Parent process can write to file and console, and wherever.
See Creating a Child Process with Redirected Input and Output on MSDN for full details.

- 28,528
- 3
- 51
- 88
Pipes are difficult to get right, especially in Windows. When you have simple one-line output, as in this case, just redirect the process' standard output to a temporary file and read that. A very simple way to redirect is to let cmd.exe
do the job, via its >
operator.
It sounds as if you are developing the started process.
If that's the case, consider using the process exit code instead of output, for a yes/no result.
The process exit code is the result value from main
. The (usually identical) values 0
and EXIT_SUCCESS
mean success, like "OK", and the value EXIT_FAILURE
, plus, in Windows, the value E_FAIL
from <windows.h>
, mean failure, like "Not OK". In Windows EXIT_SUCCESS
(from the <stdlib.h>
header) is always 0, and EXIT_FAILURE
is usually 1.
It's much simpler and more efficient to check a process exit code than to check a process' output.
In particular, the C++ standard library's system
function returns the process exit code, and passes your command to cmd.exe
, so there's no need to do the API-level dance for this case.

- 142,714
- 15
- 209
- 331
-
Note that in order for this to work, you will have to explicitly launch the process via `cmd.exe /c`. The `CreateProcess` API does not support standard-output redirection directly. – Cody Gray - on strike Dec 13 '16 at 14:18
-
@CodyGray: Not "explicitly", because `system` does that job for you. But otherwise, yes. Thanks for pointing that out (not sure if it needs to be in the answer, ATM I see it as footnote-like detail). – Cheers and hth. - Alf Dec 13 '16 at 14:24
-
Well yeah, `system` will do it. But I wouldn't use that with a gun to my head. The question does explicitly mention CreateProcess. – Cody Gray - on strike Dec 13 '16 at 14:25
-
@CodyGray: I am aware that elsewhere on SO there's an answer with some FUD about `system`. And I guess that's what disinformed you about `system`. There's too much of such advice around, so, just in case that's not your source: what (else) makes you wary of using `system`? – Cheers and hth. - Alf Dec 13 '16 at 14:27
-
2I haven't read the answer about it. `system` allows you to pretend that you are writing "standard" platform-independent code, but you aren't really. There are virtually no commands you can execute at the command line that will be the same on all operating systems. So if you're writing platform-dependent code anyway, you might as well use the platform APIs. These give you significantly more control over the behavior, allowing you to do nice things like redirecting input without resorting to legacy DOS syntax and all of its limitations. – Cody Gray - on strike Dec 13 '16 at 14:30
-
"you might as well" sounds like it needs fleshing out. Windows' `CreateProcess` is much more complex to use than `system`. – Cheers and hth. - Alf Dec 13 '16 at 14:33
-
1If `CreateProcess` is too hard to use then I can see the appeal of `system`. And for toy programs then it makes sense. But if I wanted to pipe output from an external process I am sure I'd want to start that process directly rather than put cmd.exe in between, and as Cody says have to deal with the vagaries of cmd's command parser. Much less tangible, getting cmd to do the work just makes me feel lazy and dirty. – David Heffernan Dec 13 '16 at 15:07
-
@CodyGray: It's true there are few commands that are shared between Posix and Windows platforms - but if you are just trying to run an executable which you wrote, it's really very simple - not entirely platform independent, but a *lot* easier to deal with than CreateProcess/fork+exec. – Martin Bonner supports Monica Dec 14 '16 at 08:23