Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to redirect output message in windows command prompt (cmd.exe)

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.

like image 436
TimMe Avatar asked Nov 03 '11 06:11

TimMe


1 Answers

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.

like image 173
Harry Johnston Avatar answered Oct 30 '22 20:10

Harry Johnston