Win32 in C - Why is my text displayed as foreign?

The win32 API will start viewing on this site: http://www.winprog.org/tutorial/start.html

I literally just compiled the first example, and it gave me a message in Chinese / Japanese or something like that.

Question: Why?

Obviously, as I understand it, I should get "Goodbye, cruel world!". in the message box (presumably called "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.

+8
c winapi locale
source share
3 answers

Try changing the code as follows:

 #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 this works because you are missing the header indicating the correct API, you seem to be calling MessageBoxW (version of Unicode) using the ANSI string. If this is not just a test, but you are starting to write a real-world program, think about what type of framework you want to use (usually this is the flag of the precompiler). Then use the _T( macro _T( so that your literals are compatible as with unicode / ansi.

Edit comment @Benoit: Starting a new project with VS 2008/10 sets the default Unicode character set.

+9
source share
 MessageBox(NULL, _T("Goodbye, cruel world!"), _T("Note"), MB_OK); 

or

 MessageBoxA(NULL, "Goodbye, cruel world!", "Note", MB_OK); 
+5
source share

I cannot set it by default, so every new project must be installed. To find the setting: Using Visual Studio 2010 From the main menu โ†’ Projects โ†’ Properties โ†’ Configuration Properties โ†’ General โ†’ Project Details โ†’ Character Set โ†’ โ€œUse Multibyte Character Setโ€ (set to โ€œUse Unicode Character Setโ€)

After that, everything looks good.

+1
source share

All Articles