简体   繁体   English

如何在启动时重定向stderr,而无需重定向stdin和stdout?

[英]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.

相关问题 如何正确启动进程并转发stdin / stdout / stderr? - How do I correctly launch a process and forward stdin/stdout/stderr? 如何在 Windows 上的 CreateProcess() 中不继承标准输入、标准输出和标准错误 - How to NOT inherit stdin, stdout and stderr in CreateProcess() on Windows 在Windows上重定向stdout / stderr - Redirecting stdout/stderr on Windows 在Windows中,如何创建子进程并捕获其stdin,stdout和stderr,而无需复制任何可继承的句柄? - In Windows, how can one create a child process and capture its stdin, stdout, and stderr, without duplicating any inheritable handles? 如何将 Windows cmd stdout 和 stderr 重定向到单个文件? - How to redirect Windows cmd stdout and stderr to a single file? 如何在 Windows 中访问除 stdout、stderr 和 stdin 之外的继承匿名管道 HANDLE? - How to access an inherited anonymous pipe HANDLE, other than stdout, stderr & stdin, in Windows? 如何使用 boost.process 重定向标准输入和标准输出 - how to redirect stdin and stdout using boost.process 没有stdout或stderr的命令行工具如何打印到控制台? - How could a command line tool print to console without stdout or stderr? 无法在Windows中重定向可执行文件的stdout或stderr - Failing to redirect stdout or stderr of an executable in windows 使用open3的perl写入STDIN并捕获STDOUT和STDERR - perl using open3 to write to STDIN and capture STDOUT and STDERR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM