Support for WinAPI and UTF-8

A quick question regarding UTF-8 support and various Win32 APIs.

In a typical C ++ MFC project, is it possible for MessageBox () to display a UTF-8 encoded string?

Thanks Andrew

+6
c ++ winapi utf-8 localization mfc
source share
3 answers

Quick answer: None.

The answer is longer: it will work if the string contains only regular ANSI characters, such as US English, since these character codes are the same in UTF-8 and ANSI.

If characters other than ANSI are included, or any double-byte encoded characters, you need to convert to Unicode-16 using MultiByteToWideChar with CP_UTF8. Your program must also be compiled using UNICODE, or you can use the API calls "W" - for example, MessageBoxW.

(Note that functions that take a text argument, such as MessageBox, CreateWindow, are mapped to versions "A" or "W" depending on whether UNICODE is defined).

It may also be helpful;

http://www.joelonsoftware.com/articles/Unicode.html

+8
source share

No, use MultiByteToWideChar with CP_UTF8 . See http://www.siao2.com/2006/10/11/816996.aspx for why A cannot do this; W (UCS-2) is the only alternative.

+4
source share

I use ATL / MFC string conversion macros. For example, if you have an ASCII string called myUTF8Str containing UTF8 characters:

 ::MessageBox(hWnd, CA2T(myUTF8Str, CP_UTF8), _T("Caption"), MB_OK); 

Alternatively, you can create an instance of a string, for example:

 CA2T myConvertedString(myUTF8Str, CP_UTF8); ... TRACE(_T("Converted: %s\n"), myUTF8Str.m_psz); 

Notice the m_psz element, which allows read-only access to the raw string pointer.

You can also code using CT2A , for example:

 CT2A myEncodedString("Some UTF8", CP_UTF8); 

If you are not using TEXT macros, use CA2W, CW2A, etc.

+3
source share

All Articles