Discussion:
Detecting Console Redirection
(too old to reply)
D***@gmail.com
2008-02-05 13:11:03 UTC
Permalink
I have a problem and wonder if anyone here can help.

I have been writing a number of Win32 Console applications and instead
of using Writeln, I've gone to the Win32 API and used GetStdHandle,
WriteConsoleOutputAttribute and WriteConsoleOutputCharacter so that I
can output coloured text (if required, errors in red etc).

These application seem to work well in 4NT and CMD but they have a
problem with being run from within the BDS 2006 IDE via a Open Tools
package which I'm writing. The package is designed to run command-line
tools before and after compiles, i.e. unit tests, etc.

What actually happens is that the GetStdHandle() function returns a
handle that isn't acceptable to the other functions (only managed to
find this out using EurekaLog, an excellent tool if anyone's
interested). After reading the Win32 API information on console
applications one paragraph states that WriteConsole, etc will not work
on redirected input and output and you should use WriteFile, etc. The
use of Writeln works ok - I assume it uses WriteFile although it seems
to be written in assembler.

My question is, is there a way to detect IO redirection from within
the console application and therefore use the basic functions
Writeln() etc, instead of the Win32 API functions.

My goal for all of this is to use the Open Tools package functionality
with console applications to provide integrated testing of units, GUIs
and update various ZIP and XML files as well. Ambituous, yes, but so
far its working except for the above.

regards
Dave.
Dave
2008-03-12 16:39:44 UTC
Permalink
To All,

After reading the Win32 SDK several times I've found the answer. If
GetConsoleMode() fails, (returns = 0) then the console is redirected.

regards
Dave.
Post by D***@gmail.com
I have a problem and wonder if anyone here can help.
I have been writing a number of Win32 Console applications and instead
of using Writeln, I've gone to the Win32 API and used GetStdHandle,
WriteConsoleOutputAttribute and WriteConsoleOutputCharacter so that I
can output coloured text (if required, errors in red etc).
These application seem to work well in 4NT and CMD but they have a
problem with being run from within the BDS 2006 IDE via a Open Tools
package which I'm writing. The package is designed to run command-line
tools before and after compiles, i.e. unit tests, etc.
What actually happens is that the GetStdHandle() function returns a
handle that isn't acceptable to the other functions (only managed to
find this out using EurekaLog, an excellent tool if anyone's
interested). After reading the Win32 API information on console
applications one paragraph states that WriteConsole, etc will not work
on redirected input and output and you should use WriteFile, etc. The
use of Writeln works ok - I assume it uses WriteFile although it seems
to be written in assembler.
My question is, is there a way to detect IO redirection from within
the console application and therefore use the basic functions
Writeln() etc, instead of the Win32 API functions.
My goal for all of this is to use the Open Tools package functionality
with console applications to provide integrated testing of units, GUIs
and update various ZIP and XML files as well. Ambituous, yes, but so
far its working except for the above.
regards
Dave.
Loading...