GetConsoleScreenBufferInfoEx, return value, GetLastError? #12069

Closed
opened 2026-01-31 03:05:33 +00:00 by claunia · 14 comments
Owner

Originally created by @vefatica on GitHub (Jan 14, 2021).

Environment

v:> ver
TCC 27.00.18 x64
Microsoft Windows 10 Pro for Workstations
10.0.19042.746 (2009, 20H2)
WindowsTerminalPreview_1.5.3242.0_x64

Windows build number: [run `[Environment]::OSVersion` for powershell, or `ver` for cmd]
Windows Terminal version (if applicable):

Any other software?

Steps to reproduce

See below.

Expected behavior

Something else.

Actual behavior

Here's some code I'm using to test an unrelated ussue. Sorry it uses a bit of plugin interface language. You'll get the gist.

	CONOUT conout;
	CONSOLE_SCREEN_BUFFER_INFOEX csbiex = {sizeof(CONSOLE_SCREEN_BUFFER_INFOEX), 0};
	if ( GetConsoleScreenBufferInfoEx(conout.h, &csbiex) == FALSE );
		TCError(GetLastError(), NULL);
	for ( INT i=0; i<16; i++ )
		Printf(L"Color %d = 0x%06lX\r\n", i, csbiex.ColorTable[i]);

If it's run in WT, in OpenConsole, or in conhost it produces (snipped) ...

image

... apparently succeeding but returning FALSE with GetLastError() == 0.

Originally created by @vefatica on GitHub (Jan 14, 2021). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment v:\> ver TCC 27.00.18 x64 Microsoft Windows 10 Pro for Workstations 10.0.19042.746 (2009, 20H2) WindowsTerminalPreview_1.5.3242.0_x64 ```none Windows build number: [run `[Environment]::OSVersion` for powershell, or `ver` for cmd] Windows Terminal version (if applicable): Any other software? ``` # Steps to reproduce <!-- A description of how to trigger this bug. --> See below. # Expected behavior Something else. <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> # Actual behavior <!-- What's actually happening? --> Here's some code I'm using to test an unrelated ussue. Sorry it uses a bit of plugin interface language. You'll get the gist. ``` CONOUT conout; CONSOLE_SCREEN_BUFFER_INFOEX csbiex = {sizeof(CONSOLE_SCREEN_BUFFER_INFOEX), 0}; if ( GetConsoleScreenBufferInfoEx(conout.h, &csbiex) == FALSE ); TCError(GetLastError(), NULL); for ( INT i=0; i<16; i++ ) Printf(L"Color %d = 0x%06lX\r\n", i, csbiex.ColorTable[i]); ``` If it's run in WT, in OpenConsole, or in conhost it produces (snipped) ... ![image](https://user-images.githubusercontent.com/61856645/104546488-4e5f8580-55fa-11eb-81b6-9a1201ccbb50.png) ... apparently succeeding but returning FALSE with GetLastError() == 0.
claunia added the Needs-TriageResolution-ExternalNeeds-Tag-FixNeeds-Attention labels 2026-01-31 03:05:34 +00:00
Author
Owner

@vefatica commented on GitHub (Jan 14, 2021):

And those color values look more like RGBs than COLORREFs.

@vefatica commented on GitHub (Jan 14, 2021): And those color values look more like RGBs than COLORREFs.
Author
Owner

@vefatica commented on GitHub (Jan 14, 2021):

As for the return value ... my mistake (misplaced semicolon) ... sorry.

But are they supposed to look like RGBs? Color 1 is red and red's COLORREF is 0x000080.

@vefatica commented on GitHub (Jan 14, 2021): As for the return value ... my mistake (misplaced semicolon) ... sorry. But are they supposed to look like RGBs? Color 1 is red and red's COLORREF is 0x000080.
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Jan 14, 2021):

Color 1 is red

But the CHAR_INFO structure documentation says FOREGROUND_BLUE is 0x0001.

@KalleOlaviNiemitalo commented on GitHub (Jan 14, 2021): > Color 1 is red But the [CHAR_INFO structure](https://docs.microsoft.com/en-us/windows/console/char-info-str) documentation says `FOREGROUND_BLUE` is 0x0001.
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Jan 14, 2021):

The color numbering of the Windows console API originates from the IBM CGA color palette, I think. It's not the same as what is used in SGR control sequences.

@KalleOlaviNiemitalo commented on GitHub (Jan 14, 2021): The color numbering of the Windows console API originates from the [IBM CGA color palette](https://en.wikipedia.org/wiki/Color_Graphics_Adapter#Color_palette), I think. It's not the same as what is used in [SGR control sequences](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting).
Author
Owner

@DHowett commented on GitHub (Jan 14, 2021):

Just to confirm: your return value issue was caused by user error?

@KalleOlaviNiemitalo is correct. The colors are not indexed the same as in VT. The colors match the values you get if you OR together the FOREGROUND_ constants.

@DHowett commented on GitHub (Jan 14, 2021): Just to confirm: your return value issue was caused by user error? @KalleOlaviNiemitalo is correct. The colors are not indexed the same as in VT. The colors match the values you get if you OR together the `FOREGROUND_` constants.
Author
Owner

@vefatica commented on GitHub (Jan 14, 2021):

Yes, indeed! ... user error on the return value part.

What's CHAR_INFO got to do with it?

The docs say GetConsoleScreenBufferInfoEx gives an array of COLORREFs and the docs for COLORREFs give examples where red is in the low byte. Is the meaning of COLORREF subject to interpretation?

@vefatica commented on GitHub (Jan 14, 2021): Yes, indeed! ... user error on the return value part. What's CHAR_INFO got to do with it? The docs say GetConsoleScreenBufferInfoEx gives an array of COLORREFs and the docs for COLORREFs give examples where red is in the low byte. Is the meaning of COLORREF subject to interpretation?
Author
Owner

@DHowett commented on GitHub (Jan 14, 2021):

No, COLORREF is always BGR.

The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue.

The low-order byte is the last one in the hex-text representation 0x00BBGGRR.

The CHAR_INFO structure docs, as linked by @KalleOlaviNiemitalo, list the values of the color constants. Are you looking for a way in which it has even more to do with the color ordering? 😉

@DHowett commented on GitHub (Jan 14, 2021): No, COLORREF is always BGR. > The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The low-order byte is the last one in the hex-text representation `0x00BBGGRR`. The `CHAR_INFO` structure docs, as linked by @KalleOlaviNiemitalo, **list the values of the color constants.** Are you looking for a way in which it has _even more_ to do with the color ordering? :wink:
Author
Owner

@vefatica commented on GitHub (Jan 14, 2021):

Try again ... GetConsoleScreenBufferInfoEx uses a CONSOLE_SCREEN_BUFFER_INFOEX, one member of which is

COLORREF ColorTable[16];

The numbers that GetConsoleScreenBufferInfoEx puts in that array are in the format 0x00RRGGBB which is not the format of a COLORREF.

@vefatica commented on GitHub (Jan 14, 2021): Try again ... GetConsoleScreenBufferInfoEx uses a CONSOLE_SCREEN_BUFFER_INFOEX, one member of which is `COLORREF ColorTable[16];` The numbers that GetConsoleScreenBufferInfoEx puts in that array are in the format 0x00RRGGBB which is not the format of a COLORREF.
Author
Owner

@zadjii-msft commented on GitHub (Jan 14, 2021):

Wait I'm not so sure about that.

ColorTable[1] in the console is DARK_BLUE, so 0x800000 as a 0xBBGGRR color would be rgb(0, 0, 128). Right?

It might be easier to debug by changing the colors first a little, so red is something like 0x40, green as 0x60, blue as 0x80, so you'd know exactly which ones were getting returned.

@zadjii-msft commented on GitHub (Jan 14, 2021): Wait I'm not so sure about that. ColorTable[1] in the console is `DARK_BLUE`, so `0x800000` as a `0xBBGGRR` color would be `rgb(0, 0, 128)`. Right? It might be easier to debug by changing the colors first a little, so red is something like 0x40, green as 0x60, blue as 0x80, so you'd know exactly which ones were getting returned.
Author
Owner

@vefatica commented on GitHub (Jan 14, 2021):

Thanks, Mike. I experimented a bit with the values in HKCU\Console. User error again. I don't know where I got the idea that ColorTable 1,2,4 were R,G,B ... maybe from the 31, 32, 34 used in the control sequences (but those didn't come from MS). Sorry for bugging you guys.

@vefatica commented on GitHub (Jan 14, 2021): Thanks, Mike. I experimented a bit with the values in HKCU\Console. User error again. I don't know where I got the idea that ColorTable 1,2,4 were R,G,B ... maybe from the 31, 32, 34 used in the control sequences (but those didn't come from MS). Sorry for bugging you guys.
Author
Owner

@zadjii-msft commented on GitHub (Jan 14, 2021):

No worries, glad you got it figured out! The Windows Console is definitely the odd one out with the color table being in BGR order, and the COLORREFs being BBGGRR. Everyone else on the planet has them in RGB order 🤷‍♂️

@zadjii-msft commented on GitHub (Jan 14, 2021): No worries, glad you got it figured out! The Windows Console is definitely the odd one out with the color table being in BGR order, _and_ the COLORREFs being BBGGRR. Everyone else on the planet has them in RGB order 🤷‍♂️
Author
Owner

@DHowett commented on GitHub (Jan 16, 2021):

Try again

I’m glad you got your problem sorted out, but I have to tell you that this comes off as particularly unkind. I am here trying to help and point you towards relevant documentation[1], but the page Kalle linked being a few short hops away from does not really mean that I should “try again.” If I fail to provide you details you find relevant or interesting, you can say that without ordering me to take twenty.

I know you probably didn’t intend that, but I figured I’d let you know how it reads.

1: we all use a common infrastructure here in the console; the color orders are dictated by history and documented with CHAR_INFO. The table you called out would therefore be in the order documented for other parts of the console, right? 😄

@DHowett commented on GitHub (Jan 16, 2021): > Try again I’m glad you got your problem sorted out, but I have to tell you that _this_ comes off as particularly unkind. I am here _trying_ to help and point you towards relevant documentation[1], but the page Kalle linked being a few short hops away from does not really mean that I should “try again.” If I fail to provide you details you find relevant or interesting, you can say that without ordering me to take twenty. I know you probably didn’t intend that, but I figured I’d let you know how it reads. 1: we all use a common infrastructure here in the console; the color orders are dictated by history and _documented with CHAR_INFO_. The table you called out would therefore be in the order documented for other parts of the console, right? :smile:
Author
Owner

@vefatica commented on GitHub (Jan 16, 2021):

I may have been adamant (thinking I was right). I never mean to be unkind. I'm sorry if you took offense. I have great respect for the members of the Windows Terminal team.

@vefatica commented on GitHub (Jan 16, 2021): I may have been adamant (thinking I was right). I never mean to be unkind. I'm sorry if you took offense. I have great respect for the members of the Windows Terminal team.
Author
Owner

@DHowett commented on GitHub (Jan 16, 2021):

We like you too 😉 you keep us on our toes!

Thanks
d

@DHowett commented on GitHub (Jan 16, 2021): We like you too 😉 you keep us on our toes! Thanks d
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12069