简体   繁体   English

管道使用WriteConsole的程序

[英]Piping a program that uses WriteConsole

I wanted to call _popen to get the results from an executable but it was blanking out. 我想调用_popen从可执行文件中获取结果,但它是在消隐。

I looked in debugger and found out the program uses Kernel32.WriteConsoleW to write a unicode string to the console, instead of using stdout. 我查看调试器,发现程序使用Kernel32.WriteConsoleW将unicode字符串写入控制台,而不是使用stdout。

How do I capture it? 我该如何捕获它?

Any output generated with WriteConsole will not be written to the redirection pipe. 使用WriteConsole生成的任何输出都不会写入重定向管道。 If a handle to a pipe or a file or anything else than a console is given to WriteConsole , it will fail with ERROR_INVALID_HANDLE (6). 如果给WriteConsole一个管道或文件的句柄或控制台以外的任何其他东西,它将失败并返回ERROR_INVALID_HANDLE (6)。

From High-Level Console Input and Output Functions on MSDN: 从MSDN上的高级控制台输入和输出功能

ReadConsole and WriteConsole can only be used with console handles; ReadConsole和WriteConsole只能用于控制台句柄; ReadFile and WriteFile can be used with other handles (such as files or pipes). ReadFile和WriteFile可以与其他句柄(例如文件或管道)一起使用。 ReadConsole and WriteConsole fail if used with a standard handle that has been redirected and is no longer a console handle. 如果与已重定向且不再是控制台句柄的标准句柄一起使用,则ReadConsole和WriteConsole会失败。

The overkill solution: intercept calls to WriteConsoleW by hooking into the application on start. overkill解决方案:通过在启动时挂接到应用程序来拦截对WriteConsoleW的调用。 Probably not what you're looking for, and I'm sure there's an easier way. 可能不是你想要的,我相信有一个更简单的方法。 But it'll work for sure :) 但它肯定会起作用:)

You should be able to redirect the Output of a child process. 您应该能够重定向子进程的输出。

Have a look at Creating a Child Process with Redirected Input and Output 看一下使用重定向输入和输出创建子进程

Furthermore the application might use STD_ERROR_HANDLE instead of STD_OUTPUT_HANDLE. 此外,应用程序可能使用STD_ERROR_HANDLE而不是STD_OUTPUT_HANDLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM