I tried wchar_t, char* and LPCSTR, all give the same result.
Thanks in advance.
Here is the high-level reason why your code is not working:
The MessageBox() is an ANSI function, not a Unicode aware function. Unless you change the call to MessageBox() to a Unicode-aware function, you can try all sorts of combinations and the results will be the same. In other words, an ANSI function isn't going to magically process Unicode strings correctly.
I say this is the high-level reason, since it can occur with any function you call that only knows about ANSI strings. Give that function a Unicode string, and nothing will make that function act differently.
Now as to the detailed reason -- the MessageBox() function is not really a function -- it is a macro. If your build is an ANSI build, the function is really MessageBoxA. If you have a Unicode build, then the function is MessageBoxW. So you probably have your project set to ANSI (or MBCS), causing the wrong version of MessageBox() to be called.
So you have a choice -- change your build to Unicode (IMO preferred method), or call the MessageBoxW() function directly.
Also, look at the MessageBox() description, and the second/third parameter:
Thanks, I tried those things and none worked. Looks like it has nothing to do with that.
Why not tell us what exactly is sitting at that address that is returned instead of us having to guess?
Use your debugger -- go to that address and see what is at that memory location and the subsequent memory locations. If you expect "Hello" as the string, is it:
'H', 0, 'e', 0, 'l', 0, 'l', 0, 'o', 0
In other words, a 0 byte is between each letter? If it is, then that is a Unicode string, and you did not do as stated in my previous post (the MessageBox(W) function would have worked). Also, if you changed your build to a Unicode build, you would have had a compiler error with the code you have now, as the third parameter to MessageBox would have been incorrect (you are sending a char*, and the third parameter expects a wide character string).
Another thing -- use LPCTSTR, TCHAR, _T(), TEXT(), etc.
Last edited by Paul McKenzie; June 11th, 2012 at 07:30 AM.
1) You declared messs to be a char*, when obviously the string you're trying to get from the resource is a wide string. That's why I stated you use LPCTSTR, not char*
2) You cannot cast non-wide strings to wide strings or vice-versa.. Look at the third parameter to MessageBox. Casting from one string representation to another doesn't work. Strings must be converted from one type to another using the WideCharToMultiByte or MultiByteToWideChar functions. A C-style cast does none of that. You make the same attempt on the second parameter (going back to item 1 on the list). So your entire call to MessageBox() is bad.
Here is what you're supposed to do:
1) Make your project Unicode.
2) Make all of your character variables follow the Windows types for Unicode applications (LPCTSTR, TCHAR, _T(), TEXT) -- there should be no char*, LPCSTR, etc.
3) String literals must already be Unicode, and not ANSI strings being falsely casted to wide-strings:
while that string was not even from the resources but straight (LPCWSTR)"bonjour"
As my post pointed out that code is invalid.
You are taking an ANSI string, and believing that a "dumb" C-style cast knows how to turn that ANSI string into a wide string. All that cast did was to shut the compiler up. Strings must be converted, and you have to call conversion functions to do this.