andreihondrari
andreihondrari

Reputation: 5833

winprog.org first example strange behavior?

I am talking about winprog.org's tutorial on windows GUI because many users on StackO recommended it.

I basically tried this example:

#include "stdafx.h"
#include "winapiTest1.h"


int WINAPI _tWinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPTSTR    lpCmdLine,
        int       nCmdShow)
{
    MessageBox(NULL, (LPCWSTR) "Goodbye, cruel world!", (LPCWSTR) "Note", MB_OK);
    return 0;
}

But when I run it, it shows the message box but every string in it (even the title) is represented in Chinese characters.

What seems to be the problem?

Upvotes: 1

Views: 259

Answers (2)

In silico
In silico

Reputation: 52217

But when I run it, it shows the message box but every string in it (even the title) is represented in Chinese characters.

The reason why is because the operating system is interpreting your string literals as if they were encoded using UTF-16, even though they're not.

Some identifiers like MessageBox() comes in two flavors: a MessageBoxA() function and a MessageBoxW(). MessageBoxA() is the "ANSI" version that accept strings encoded in the current code page, and MessageBoxW() is the "wide" version that accept strings encoded in UTF-16 (i.e. it is "Unicode-aware"). MessageBox() is actually a macro that defines to either one of those depending on whether the UNICODE macro is defined. There is no actual function in the Windows API that is called MessageBox() per se.

The crux of the problem is here:

MessageBox(NULL, (LPCWSTR) "Goodbye, cruel world!", (LPCWSTR) "Note", MB_OK); 

Your project probably has a #define UNICODE somewhere, causing the MessageBox() identifier to resolve to MessageBoxW(), which expects a UTF-16 encoded string. Normally, the compiler would have complained to you about it (i.e. compilation error), but the LPCWSTR cast essentially shuts the compiler up and forces it to compile.

You cannot just throw in a random cast and always expect it to work. Unfortunately lots of Windows API tutorials (and some C++ tutorials in general) abuse casts all over the place when they're not necessary or even when they're inappropriate.

The solution that James propose uses the _T() macros, which help set it so the string literal is properly encoded, without requiring casting. However, a superior solution for new applications is to forget about the ANSI versions of the functions and simply work in Unicode the entire time. The _T() macro is a stopgap for applications transitioning to the Unicode-aware APIs.

#include "stdafx.h"   // Make sure UNICODE is defined before windows.h       
#include "winapiTest1.h"          

int WINAPI WinMain(HINSTANCE hInstance,          
        HINSTANCE hPrevInstance,          
        LPCSTR    lpCmdLine,          
        int       nCmdShow)          
{          
    MessageBox(NULL, L"Goodbye, cruel world!", L"Note", MB_OK);          
    return 0;          
}         

Note that the string literals now have an L prefixed to them. This tells the Visual C++ compiler to interpret the literal as a UTF-16 encoded string (note that it might turn it into a different encoding on other C++ compilers; this applies to Visual C++ only).

Also note that the above advice applies only when working with only the Windows API. Some frameworks and libraries that wrap the Windows API has their own provisions for handling Unicode strings. Follow those instead, if you are using a framework/library.

Upvotes: 5

James M
James M

Reputation: 16728

Try:

MessageBox(NULL, _T("Goodbye, cruel world!"), _T("Note"), MB_OK);

Upvotes: 1

Related Questions