I wrote a program that displays Hello World in the MessageBox window. Compiled in MSVS 2008, but derived some Japanese, or Chinese characters (literally). What could be the problem:

#include<windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, (LPCWSTR) "Hello, World!!!", (LPCWSTR) "Hello from MessageBox", MB_OK); return 0; } 
  • Sometimes there are similar problems when copying text. - Fairlooli 1:09 pm
  • four
    The problem is this: (LPCWSTR) . No need to use explicit C-type casting. With this, you just gag the compiler, which would clearly curse that cannot char [] to wchar_t *. As correctly noted, it is better to use the _T macro and the TCHAR type to write portable programs. Another option is to use CString, which can be converted where necessary (although you need to carefully handle the built-in buffer). - gecube 3:12 pm

3 answers 3

As in one of your previous questions, try using the TCHAR type:

 #include <tchar.h> 

and

 MessageBox( NULL, _T("Hello, World!!!"), _T("Hello from MessageBox"), MB_OK ); 

    The problem with the encoding. Try to specify in project properties that it is not necessary to use unicode.

    • First, the opposite is necessary, and secondly, there is a caste, so the problem is not quite with the encoding. - Qwertiy

    MS VS2010: Project -> Properties -> Configuration Properties -> General -> Character Set = "Use Unicode Character Set":

     MessageBox( NULL, (LPCWSTR) TEXT("Hello, World!!!"), (LPCWSTR) TEXT("Hello from MessageBox"), MB_OK ); 

    or

     MessageBox( NULL, TEXT("Hello, World!!!"), TEXT("Hello from MessageBox"), MB_OK ); 
    • It does not help, there is an incorrect caste. - Qwertiy
    • It works for me. Apparently auto-transform. But the second option - no problem - Spectrum
    • In the MessageBox variant (NULL, (LPCWSTR) "Hello, World !!!", (LPCWSTR) "Hello from MessageBox", MB_OK); I also have hieroglyphs in the message box, with Unicode. But changing Unicode to something else is not always possible or difficult, but not for this example question, of course - Spectrum
    • And for the original: "Hello, World !!!" - as hieroglyphs, and "Hello from MessageBox" - displayed normally after hieroglyphs. And in the title of the window are also irregulars, but others ... In response with _T (,,,) it is more difficult - in addition you need to add #include <tchar.h> - Spectrum
    • The second option is ok. And the first one is not, because it works only in one of two cases. - Qwertiy