Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping a program that uses WriteConsole

Tags:

c++

stdout

pipe

I wanted to call _popen to get the results from an executable but it was blanking out.

I looked in debugger 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?

like image 670
user202987 Avatar asked Sep 25 '11 03:09

user202987


1 Answers

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.

like image 136
Felix Dombek Avatar answered Sep 23 '22 14:09

Felix Dombek