anastaciu
anastaciu

Reputation: 23792

MessageBox does not print UNICODE characters

I'm using the following to print a message in a Win32 API MessageBox:

MessageBox(hWnd, TEXT("Já existe um controlador em execução"), TEXT("Erro"), 0);

MessageBox is a macro and is expanding to MessageBoxW. The trouble is that it doesn't print Unicode, whereas the window that calls it prints Unicode without any issue, it seems that this is a problem with MessageBox itself.

Does anyone know how to solve this?

FYI, I also tried:

MessageBoxEx(hWnd, TEXT("Já existe um controlador em execução"), TEXT("Erro"), 0, MAKELANGID(LANG_PORTUGUESE, SUBLANG_PORTUGUESE));

But it's the same, as expected.

Here is a picture of the call with the expansion:

enter image description here

And it prints:

enter image description here

Note that the main window menu has unicode characters that are printed correctly.

Upvotes: 3

Views: 1511

Answers (2)

Iziminza
Iziminza

Reputation: 389

If you do not want to encode your Unicode characters as escape sequences, make sure your source code editor uses the same encoding as your compiler.

What you experience is that you see Unicode characters, but the wrong ones. This is called "Mojibake" and happens whenever you (or your compiler!) interprets a file using a different encoding (likely some iso-8859-? encoding) than your editor (utf-8).

You could configure your compiler to use utf-8 as well. If you are using gcc, you might want to read the accepted answer in this SO post.

Upvotes: 0

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29945

To avoid source-code encoding based problems in the future, you can use \uxxxx style escape characters for non-ascii characters:

MessageBoxW(nullptr, L"J\u00E1 existe um controlador em execu\u00E7\u00E1o", L"Erro", MB_OK);

Upvotes: 4

Related Questions