开发者

Capturing the effects of SetConsoleTextAttribute when redirected through a Pipe?

开发者 https://www.devze.com 2022-12-19 03:11 出处:网络
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

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开发者_如何学Python 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号