C++ : Why this window title gets truncated?

The problem in your code is that you are using DefWindowProc instead of DefWindowProcW. Changing that will fix the code.

Ideally you should change your project settings to use Unicode, not multi-byte character set. This will simplify everything and you can use the macros like CreateWindowEx and RegisterClassEx instead of explicitly using the Unicode / ANSI versions as you are.

As others have said, this is a mismatch between character sets.

You should ideally match character sets between all your API calls that interact with each other. So if you use CreateWindowExW you should also use RegisterClassExW, DefWindowProcW, DispatchMessageW...


CreateWindowExA interprets the string as 8 bit characters. The second 8 bits of L"Sample" is zero, because its first character is 0x0053 - the L means use wide characters. So the function interprets that as a 1 character null terminated string.


This is a very nice one, learned something new!

You need to change

return DefWindowProc(hWnd, uMsg, wParam, lParam);  

to

if(IsWindowUnicode(hWnd))  
  return DefWindowProcW(hWnd, uMsg, wParam, lParam);  
else  
  return DefWindowProcA(hWnd, uMsg, wParam, lParam);

Or even better: stick to one character encoding. At best just use RegisterClass, CreateWindowEx and so on and let the compiler take the right Unicode or ANSI function.

Tags:

C++

Winapi