views:

131

answers:

2

I've redirected stdout of a child process spawned with CreateProcess to a pipe. It works fine except that, as far as I can tell, no information about color changes are coming through. The child process is using SetConsoleTextAttribute to change the text color--is it possible to detect this through the pipe and, if so, how?

I'm ultimately displaying the output in a RichEdit control and I would like to capture the color information if at all possible.

This is in C with the Win32 API on XP and Vista.

A: 

You probably need to use ReadConsoleOutput (and/or related ones) found here: http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx.

Hope that helps.

Moron
A: 

There maybe a work around...its old and not-used much!

  1. Use Ansi.Sys and load that.
  2. Whenever you output a text to the console, by using the Escape sequence, you can set a color around the text.
  3. Then parse the escape sequences into the equivalent for RichText Colors.

The escape sequences are standard here. Here is how to add support for ANSI.SYS into the console. And here is the official KB from Microsoft on how to do this.

For an example:

printf("\x1b[33;43Yellow on Blue\x1b[0\n");

Now, parse the bit after the \x1b[, 33 is yellow foreground, and 43 is blue background, then look up the relevant color for that and set it in the RichTextBox..

Note: \x1b[0 turns off the attribute.

Edit: This may not be the best solution as that's for legacy NTVDM's 16bit DOS command.com under XP or later. But however, I found another link to 'ansicon' here which is for pure cmd.exe 32bit console, with ANSI support.

Hope this helps, Best regards, Tom.

tommieb75
I seriously doubt this would work. You would use ANSI.SYS to get escape strings sent to stdout to produce SetConsoleTextAttribute() calls. Not the other way around.
Hans Passant
@nobugz: You're 100% correct there...oh.....dang... I realized what you meant.. :(
tommieb75