Quick question regarding UTF-8 support and various Win32 API's.
In a typical C++ MFC project, is it possible for MessageBox() to display a UTF-8 encoded string?
Thanks, Andrew
Quick question regarding UTF-8 support and various Win32 API's.
In a typical C++ MFC project, is it possible for MessageBox() to display a UTF-8 encoded string?
Thanks, Andrew
Nope, use MultiByteToWideChar with
CP_UTF8
. See http://www.siao2.com/2006/10/11/816996.aspx for why A can't do it; W (UCS-2) is the only alternative.Quick answer: No.
Longer answer: It'll work if the string only contains regular ANSI characters, e.g US English, since these character codes are the same in UTF-8 and ANSI.
If non-ANSI characters are included, or any double-byte encoded characters, you'll need to transform to Unicode-16 using MultiByteToWideChar with CP_UTF8. Your program will also need to be compiled with UNICODE defined, or you can use the 'W' API calls - e.g. MessageBoxW.
(Note that functions taking a text argument such as MessageBox, CreateWindow map to either 'A' or 'W' versions depending on whether UNICODE is defined).
This may also be of use;
http://www.joelonsoftware.com/articles/Unicode.html
I use the ATL/MFC string conversion macros. For example, if you have an ASCII string called
myUTF8Str
containing UTF8 chars:Alternatively you can create an instance of the string, e.g.:
Note the
m_psz
member that allows read-only access to the raw string pointer.You can also encode using
CT2A
, e.g.:If you don't use TEXT macros, then use CA2W, CW2A, etc.