Piping a program that uses WriteConsole
I wanted to call _popen to get the results from an executable but it was blanking out.
I looked in debu开发者_开发问答gger and found out the program uses Kernel32.WriteConsoleW to write a unicode string to the console, instead of using stdout.
How do I capture it?
Any output generated with WriteConsole
will not be written to the redirection pipe. 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).
From High-Level Console Input and Output Functions on MSDN:
ReadConsole and WriteConsole can only be used with console handles; ReadFile and WriteFile can be used with other handles (such as files or pipes). ReadConsole and WriteConsole fail if used with a standard handle that has been redirected and is no longer a console handle.
The overkill solution: intercept calls to WriteConsoleW by hooking into the application on start. 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.
精彩评论