MessageBox(NULL, (LPCTSTR)"Test1", (LPCTSTR)"Test1", MB_OK);
This code is popping up the window correctly, but it displays all squares. Whats wrong =(
MessageBox(NULL, (LPCTSTR)"Test1", (LPCTSTR)"Test1", MB_OK);
This code is popping up the window correctly, but it displays all squares. Whats wrong =(
The squares mean the string you passed is being interpreted with the wrong character encoding. and were on the right trail: the compiler/project settings determine whether the Win32 macros call the ANSI or the wide (Unicode) API, and silencing the compiler with casts only hides the mismatch.
Quick compile-time check (prints a message during build):
#ifdef UNICODE
#pragma message("Building with UNICODE")
#else
#pragma message("Building without UNICODE")
#endif If UNICODE/_UNICODE is defined, your calls must supply wide strings or call the ANSI API explicitly.
Two practical fixes:
MessageBoxA(NULL, "Sample text", "Caption", MB_OK); int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
std::wstring ws(n, 0);
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, &ws[0], n);
MessageBoxW(NULL, ws.c_str(), ws.c_str(), MB_OK); If squares remain after fixing the API/encoding, confirm the source text encoding (UTF-8 vs ANSI) and the font/glyph coverage used by the dialog. Avoid forcing types with casts — the compiler error/warning is trying to tell you there is an encoding mismatch.
Microsoft references:
This complements ’s pointer about project settings and ’s warning about blind casts.
Jump to Post— Salem 6,009> MessageBox(NULL, (LPCTSTR)"Test1", (LPCTSTR)"Test1", MB_OK);
Indiscriminate casting to get the compiler to STFU is a common problem.Just because you muffled the compiler doesn't make your code good.
Take out the casts, then pay really close attention to the error messages.
Jump to Post— Salem 6,009Well it would display a UNICODE string, were a UNICODE string actually passed as a parameter.
> (LPCTSTR)"Test1"
Would be stored in memory as 54 65 73 74 31 00
Now whilst 0x6554 is in the UNICODE space, it isn't going to be anything resembling Latin text.Now …
Are you compiling your program for UNICODE ?
Im just using VC++ express 2008. All default settings so I couldnt tell ya
> MessageBox(NULL, (LPCTSTR)"Test1", (LPCTSTR)"Test1", MB_OK);
Indiscriminate casting to get the compiler to STFU is a common problem.
Just because you muffled the compiler doesn't make your code good.
Take out the casts, then pay really close attention to the error messages.
http://msdn.microsoft.com/en-us/library/c426s321%28VS.71%29.aspx
Im just using VC++ express 2008. All default settings so I couldnt tell ya
Then that's probably the problem -- turn UNICODE off! To do that go to menu item Project --> Properties (the last item on that menu) --> Configuration Properties --> General. Then on the right side of the screen change Character Set (third from the bottom) to Not Set Also remove those unnecessary typecasts.
> MessageBox(NULL, (LPCTSTR)"Test1", (LPCTSTR)"Test1", MB_OK);
Indiscriminate casting to get the compiler to STFU is a common problem.Just because you muffled the compiler doesn't make your code good.
Take out the casts, then pay really close attention to the error messages.
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
lawl.
Thanks dragon
Then that's probably the problem -- turn UNICODE off! To do that go to menu item Project --> Properties (the last item on that menu) --> Configuration Properties --> General. Then on the right side of the screen change Character Set (third from the bottom) to Not Set Also remove those unnecessary typecasts.
There's something I don't understand: Why should UNICODE be off? It does contain more characters than ASCII doesn't it? What displays those characters if UNICODE doesn't?
Well it would display a UNICODE string, were a UNICODE string actually passed as a parameter.
> (LPCTSTR)"Test1"
Would be stored in memory as 54 65 73 74 31 00
Now whilst 0x6554 is in the UNICODE space, it isn't going to be anything resembling Latin text.
Now saying L"Test1" would result in 54 00 65 00 73 00 74 00 31 00 00 00
These too are valid UNICODE characters, but 0x0054 is going to display a 'T' for you.
Better still (which seems to have been lost altogether by the OP) would be to say _TEXT("Test1") Then no matter how much you mess with the "turn it on or turn it off options" in the compiler, it will do "the right thing".
Further, if the OP has any ambitions of a career, then knowing about UNICODE is pretty much a given (well knowing more than how to turn it off at any rate).
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.