why is the AfxMessageBox Displaying chinese characters
This code displays hello in chinese
char buffer[10]="hello";
AfxMessageBox((LPCTSTR)buffer);
while this code displa开发者_JAVA技巧ys it in english
AfxMessageBox(L"hello");
Could someone tell me how do i type cast the buffer variable properly if that is the problem in my code
The LPCTSTR type is a Long Pointer to a Transparent STRing. What's important here is the T, Transparent (at least I think the T stands for Transparent).
If your application is compiled as an ASCII application, all T types (like TCHAR) are redefined as their ASCII-counterpart. So TCHAR will become simply char.
If your application is compiled as Unicode, all T types are redefined as Unicode types. TCHAR becomes wchar_t.
The same is true for all Windows (and MFC) functions. All Windows functions come in 2 variants, an ASCII version (e.g. MessageBoxA) and a Unicode version (e.g. MessageBoxW). MessageBox itself is nothing more than a define to either MessageBoxA or MessageBoxW (depending on how you compile).
In your example, buffer is defined as a char-vector type, but you convert it to a pointer to a transparent type. I assume that your application is compiled in Unicode, so LPCTSTR is actually a "wchar_t *". So this cast is incorrect.
Prepending the "hello" string with L, tells the compiler to theat the constant "hello" as a Unicode string, which makes it the correct type to be passed to the Unicode version of AfxMessageBox.
If you have _UNICODE
and UNICODE
flag defined in your compiler options then casting the string LPCTSTR
will treat the string as an unicode string. In case windows, it will treat every 16 bytes as a single character and tries to find the corresponding unicode character. To make it display english character, use TCHAR
while defining the array.
精彩评论