Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set window text with Unicode special char

Tags:

c++

c

unicode

mfc

I'm trying to set dialog item text by code with the unicode special character specified below:

https://www.fileformat.info/info/unicode/char/1f310/index.htm

I have been trying calling SetWindowTextW function passing UTF-16(hex) value as parameter without success:

GetDlgItem(IDSETTINGS)->SetWindowTextW(_T("\uD83C\uDF10"));

When I build my solution I got two errors:

error C3850: '\uD83C' a universal-character-.name specifies an invalid character

error C3850: '\uDF10' a universal-character-.name specifies an invalid character

I will appreciate any kind of help.

like image 727
Alberto Bricio Avatar asked Jan 28 '23 13:01

Alberto Bricio


1 Answers

In this case the reason for compiler error C3850 can be found in the reference (emphasis mine):

Characters represented as universal character names must represent valid Unicode code points in the range 0-10FFFF. A universal character name cannot contain a value in the Unicode surrogate range, D800-DFFF, or an encoded surrogate pair. The compiler generates the surrogate pair from a valid code point automatically.

Using the UTF-32 code point works for me:

GetDlgItem( IDSETTINGS )->SetWindowTextW( L"\U0001F310" );

You can also literally store the character in the source file if you make sure the source file is stored with a Unicode encoding, I suggest to use UTF-8 with BOM.

GetDlgItem( IDSETTINGS )->SetWindowTextW( L"🌐" );

Note that you should never use the _T() nor _TEXT() macros when using a W (Unicode) API. These macros change the type of string literal depending on preprocessor variables, whereas the Unicode APIs always expect wide strings, which is enforced by using the L prefix for the string literal.

like image 78
zett42 Avatar answered Jan 31 '23 13:01

zett42