Pregunta

In Windows, I'd like to use CreatePipe and CreateProcess to launch a process, and capture the text written to stderr.

There is sample code at MSDN here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

My problem is I want to redirect and capture STDERR, but leave STDIN and STDOUT alone. Let them still be bound to the child process's console window. (If the child process is a console process and not a window process.)

If I follow the example code, and set STARTUPINFO.hStdError to my pipe handle, and set STARTUPINFO.hStdOutput and STARTUPINFO.hStdInput to INVALID_HANDLE_VALUE, then the child process has INVALID_HANDLE_VALUE to stdin and stdout; all reads fail; all printf's go nowhere; and the console window is completely blank.

Is this even possible?

¿Fue útil?

Solución

This should work:

STARTUPINFO.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
STARTUPINFO.hStdInput = GetStdHandle(STD_INPUT_HANDLE);

Alternatively, if you can't give the parent process a console, you could set hStdOutput and hStdInput to INVALID_HANDLE_VALUE and call through cmd.exe as follows:

cmd /c "subprocess.exe > con < con"

Note that SHGetFileInfo with SHGFI_EXETYPE will tell you whether the executable is a console application or not if you don't already know.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top