I tried to run the following command in Windows command prompt.
abc.exe >log.txt 2>&1
I'm expecting all output from abc.exe
to be directed to log.txt
, but it doesn't work, as the log.txt
is empty.
However, if I just execute abc.exe
, the output is showing up in Windows command prompt.
I'm not sure what is the output handler used by this application (STDOUT or STDERR), but I'm wondering is there a way to capture all messages regardless of the handler.
Addendum: as of Windows 10 v1809, Windows finally supports pseudoconsoles. If available, this offers a better solution than using the legacy console API.
If you really need to capture that message, use the console API.
CreateConsoleScreenBuffer
and SetConsoleActiveScreenBuffer
allow you to switch to a dedicated screen buffer to avoid interfering with the existing one.
SetConsoleScreenBufferSize
can make the buffer wide enough to avoid line rollover.
SetConsoleCursorPosition
can set the cursor position as required.
After you've run the program, ReadConsoleOutput
allows you to read what it wrote to the console.
You can then use GetStdHandle(STD_OUTPUT_HANDLE)
and SetConsoleActiveScreenBuffer
to return the console to the original buffer, and CloseHandle
to close your extra buffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With