Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

locale

winapi

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.

like image 806
Anonymous Avatar asked Jun 17 '11 13:06

Anonymous


3 Answers

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.

like image 74
Felice Pollano Avatar answered Nov 13 '22 00:11

Felice Pollano


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

or

MessageBoxA(NULL, "Goodbye, cruel world!", "Note", MB_OK);
like image 25
clyfish Avatar answered Nov 13 '22 00:11

clyfish


I can't get it set to default, so each new project it must be set. To find the setting: Using Visual Studio 2010 From the main Menu → Projects → Properties → Configuration Properties → General → Project Details → Character Set → "Use Multi-Byte Character set" (was set to "Use Unicode Character Set")

After that all seems well.

like image 1
Rich Avatar answered Nov 13 '22 00:11

Rich