Question

I am using a Microsoft command-line utility to perform a task. The specific details of what the utility does and the task it performs are, I think, relatively unimportant. This is what is important:

The command-line utility emits text to the console window. I cannot figure out how to redirect this text to a file, though I have tried every approach I could find through research. It seems the utility is using some strange OS function call that causes text to be printed in a way that is not subject to the normal means of redirection. I am using a Windows 7 cmd.exe console window.

None of these constructs redirect the text output in question:

util.exe >log.txt
util.exe 2>err.txt
util.exe >log.txt 2>&1
util.exe 1>log.txt 2>err.txt
util.exe 1>log.txt 2>err.txt 3>3.txt 4>4.txt 5>5.txt 6>6.txt 7>7.txt 8>8.txt 9>9.txt

Confused about that last one? Turns out there are 10 output streams in Windows-land, but that didn't get the job done either.

I can copy the text using the console window's Edit>Mark select-and-copy functionality, but I need to capture this program's output in a headless way so that it can be automated.

I cannot find any OS function call that specifically prints to the console bypassing redirection. Does such a function exist? How does this utility manage to circumvent redirection? And what means could I use to capture this utility's output?

I also tried DbgView, thinking maybe that might contain the output messages, but it did not.

The utility is not a graphical utility. It is pretty obviously a text-mode program, probably written in C or C++. It was written by Microsoft.

Was it helpful?

Solution

There's nothing particularly magical about it. Although the C library defaults to sending output to the standard output handle, there's no obligation for a program to do so; you can always open your own handle to the console output device (CONOUT$) instead.

See the documentation on CreateFile for more information about console devices.

In terms of capturing the output, you may be able to do something with ReadConsoleOutput and friends, but it isn't straightforward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top