Board index » delphi » Detecting Console Redirection
DavidGHoyle
![]() Delphi Developer |
Detecting Console Redirection2008-02-05 09:11:03 PM delphi118 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 have 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 am 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. |