Use Unicode (Use Unicode Character Set in Properties) and it will work (remove all ANSI APIs and replace char by WCHAR (and add L" for strings)) :
Combo box not showing full Unicode text when an item is selected
Issue Description
When using an ANSI application that uses the UTF-8 code page, and items in a ComboBox list contain non-English text (i.e. include characters that span multiple bytes), selecting those items results in the text being truncated in the selection field. This truncation also occurs when setting text in the ComboBox using the SetWindowTextA function.
Here is a sample program that demonstrates this issue for your reference. Compile it as 32 bit (x86) mode.
Upon further investigation and debugging into the assembly code, it appears that Windows is truncating the text before setting it in the Edit control.
Once this truncated text is present, calling the GetWindowTextLengthA function retrieves the length of the truncated text. However, there are instances where, after pasting in non-English text (like the testString2 in the sample program), the GetWindowTextLengthA function returns a value that is less than the actual number of bytes (or code units), but more than the number of actual characters (or code points). Additionally, the size returned by GetWindowTextA sometimes exceeds the value returned by GetWindowTextLengthA. This discrepancy poses a potential buffer overrun risk if GetWindowTextA attempts to write more bytes than the caller allocated.
Versions Tested
- Windows 10 Pro 22H2
- Windows 11 Pro 22H2
Workarounds
- Avoid subclassing the controls. Although Microsoft's own documentation says to subclass the edit control if you want to customize behaviour of a combo box.
- Directly set the Edit control's text using the SetWindowTextA function. One approach could be to handle the CBN_SELCHANGE notification, retrieve the string associated with the selected list item, and then set the Edit control to display that text.
Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | User experience | Other
3 answers
Sort by: Most helpful
-
-
Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
2023-09-05T06:58:05.9366667+00:00 Hello,
Welcome to Microsoft Q&A!
I agree with the communities. You should use Unicode versions instead of ANSI versions.
Refer to the Doc: Working with Strings
"but I cannot change my application to use WCHAR."
I suggest you could try to use
the MultiByteToWideChar function
converts an ANSI string to a Unicode string.You could refer to the Doc: Converting Unicode and ANSI Strings
Thank you.
Jeanine
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
-
RLWA32 49,636 Reputation points
2023-09-06T07:32:07.2833333+00:00 @David H, Since I was able to reproduce the truncated text issue with your demo when using old style subclassing it seems to me that is the core problem. Without subclassing or subclassing with SetWindowSubclass the demo seems to function as intended. I did not experience heap corruption due to buffer overruns with the demo.
Since this does not seem to be a Visual Studio or C++ issue to me I suggest you consider using the Feedback Hub to report this Windows issue to Microsoft.