Bob Dylan
Bob Dylan

Reputation: 4453

Why does my simple C++ GUI application show a message box in Chinese?

Oh as for the whole (LPCWSTR) casting thing: It wouldn't compile unless I put those in. It gave me this error message:

Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'

Upvotes: 8

Views: 4132

Answers (3)

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76531

You already have your answer, but this is a good example of why you should use avoid casts. Yes, a cast will get your code to compile, but unless you understand what is going on, most likely you've just created a bug.

In general, my 3 rules of casting are:

  1. Don't cast (fix the code).
  2. Don't cast (adjust your types to stop needing the cast)
  3. Okay, cast, but revisit your decision tomorrow and see if you can do #1 or #2.

Upvotes: 6

Roland Rabien
Roland Rabien

Reputation: 8836

Put an L infront of your string to make it a wide string. L"Goodbye cruel World"

Then you won't need the cast.

You can also use the TEXT("") macro that will create an unicode string or ascii string depending on your configuration settings.

The reason you were seeing chinese is that MessageBox was interpreting an ascii string as unicode.

Upvotes: 20

Pekka
Pekka

Reputation: 449395

My strong guess is that your source file has the wrong encoding. Can you check whether it's saved in some Unicode flavour, most likely UTF-16?

Upvotes: 0

Related Questions