Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others have pointed out in the comments, the Windows APIs do not provide direct support for UTF-8 encoded text. You cannot pass the <code>MessageBox</code> function UTF-8 encoded strings and get the output that you expect. It will, instead, interpret them as characters in your local code page.</p> <p>To get a UTF-8 string to pass to the Windows API functions (including <code>MessageBox</code>), you need to use the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072.aspx" rel="nofollow noreferrer"><code>MultiByteToWideChar</code></a> function to convert from UTF-8 to UTF-16 (what Windows calls Unicode, or wide strings). Passing the <code>CP_UTF8</code> flag for the first parameter is the magic that enables this conversion. Example:</p> <pre><code>std::wstring ConvertUTF8ToUTF16String(const char* pszUtf8String) { // Determine the size required for the destination buffer. const int length = MultiByteToWideChar(CP_UTF8, 0, // no flags required pszUtf8String, -1, // automatically determine length nullptr, 0); // Allocate a buffer of the appropriate length. std::wstring utf16String(length, L'\0'); // Call the function again to do the conversion. if (!MultiByteToWideChar(CP_UTF8, 0, pszUtf8String, -1, &amp;utf16String[0], length)) { // Uh-oh! Something went wrong. // Handle the failure condition, perhaps by throwing an exception. // Call the GetLastError() function for additional error information. throw std::runtime_error("The MultiByteToWideChar function failed"); } // Return the converted UTF-16 string. return utf16String; } </code></pre> <p>Then, once you have a wide string, you will explicitly call the wide-string variant of the <code>MessageBox</code> function, <code>MessageBoxW</code>.</p> <p>However, if you only need to support Windows and not other platforms that use UTF-8 everywhere, you will probably have a much easier time sticking exclusively with UTF-16 encoded strings. This is the native Unicode encoding that Windows uses, and you can pass these types of strings directly to any of the Windows API functions. See <a href="https://stackoverflow.com/questions/15458312/how-do-i-properly-call-the-copyfile-function-in-visual-c/15458564#15458564">my answer here</a> to learn more about the interaction between Windows API functions and strings. I recommend the same thing to you as I did to the other guy:</p> <ul> <li>Stick with <code>wchar_t</code> and <code>std::wstring</code> for your characters and strings, respectively. </li> <li>Always call the <code>W</code> variants of Windows API functions, including <code>LoadStringW</code> and <code>MessageBoxW</code>.</li> <li>Ensure that the <code>UNICODE</code> and <code>_UNICODE</code> macros are defined either before you include any of the Windows headers or in your project's build settings.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload