Search code examples
cwinapilocale

Win32 in C - Why does my text show up as a foreign language?


Started looking at the win32 API on this site: http://www.winprog.org/tutorial/start.html

I've literally just compiled the first example and it's given me a message prompt in chinese/japanese, or something along those lines.

Question: Why?

Obviously as far as my understanding goes, I should be getting "Goodbye, cruel world!" in a message box (Presumably titled 'Note').

#include <windows.h>

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

Foreign...

Thanks.


Solution

  • Try to change the code like this:

    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
    {
    MessageBox(NULL, L"Goodbye, cruel world!", L"Note", MB_OK);
    return 0;
    }
    

    If it works is because you are missing some header that point the correct API, you seem calling MessageBoxW ( the unicode version ) with an ANSI string. If this is not just a test but you are beginning to write a real world program, consider to ensure what kind of caracter you want to use ( this is a precompiler flag usually ). Then use the macro _T( to have your literals compatible both to unicode/ansi.

    Edit from @Benoit comment: Starting a new project with VS 2008/10 sets unicode character set by default.