Reputation: 3670
The Windows API part for console functions wincon.h
defines a data structure CHAR_INFO
as follows:
typedef struct _CHAR_INFO {
union {
WCHAR UnicodeChar;
CHAR AsciiChar;
} Char;
WORD Attributes;
} CHAR_INFO, *PCHAR_INFO;
So we have a union of an 8-bit and a 16-bit character denoting ASCII and Unicode characters, respectively. Usually, if you have to deal with unions in C, you have tagged unions, i.e. an extra field is present indicating which of the union's fields is being used. This is not the case here (Attributes
is used for something different), so I'm wondering how to correctly use values of this data type.
If we look at what functions of the API actually use this or similar structures, we find that it's only used by functions that exist in two variants: either suffixed with an A
(for the ASCII variant), or suffixed with a W
(for the Unicode variant).
So is it save to assume that the A
variants of these functions will only use the AsciiChar
field of this structure, and the W
variants only the UnicodeChar
field? If not, how do you know what field is actually being used and how to convert one field into the other? The MSDN documentation doesn't seem to say anything about the correct usage here.
Upvotes: 1
Views: 2092
Reputation: 8861
So is it save to assume that the A variants of these functions will only use the AsciiChar field of this structure, and the W variants only the UnicodeChar field?
Yes.
Upvotes: 2