[英]How to redirect stderr on launch *without* redirecting stdin and stdout?
In Windows, I'd like to use CreatePipe and CreateProcess to launch a process, and capture the text written to stderr. 在Windows中,我想使用CreatePipe和CreateProcess启动一个进程,并捕获写入stderr的文本。
There is sample code at MSDN here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx 在MSDN上,这里有示例代码: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms682499 (v= vs.85).aspx
My problem is I want to redirect and capture STDERR, but leave STDIN and STDOUT alone. 我的问题是我想重定向和捕获STDERR,但不要理会STDIN和STDOUT。 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; 如果我遵循示例代码,并将STARTUPINFO.hStdError设置为管道句柄,并将STARTUPINFO.hStdOutput和STARTUPINFO.hStdInput设置为INVALID_HANDLE_VALUE,则子进程将INVALID_HANDLE_VALUE设置为stdin和stdout。 all reads fail; 所有读取失败; all printf's go nowhere; 所有printf都无济于事; and the console window is completely blank. 并且控制台窗口完全空白。
Is this even possible? 这有可能吗?
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: 另外,如果您不能为父进程提供控制台,则可以将hStdOutput和hStdInput设置为INVALID_HANDLE_VALUE并通过cmd.exe进行调用,如下所示:
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. 请注意,带有SHGFI_EXETYPE
SHGetFileInfo
将告诉您可执行文件是否是控制台应用程序(如果您不知道的话)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.