Solved

Unicode characters act weirdly

Posted on 2003-11-06
12
262 Views
Last Modified: 2013-11-25
I have a task to update some MFC COM-components to support Unicode characters (Chinese text). So far I have successfully fixed two components, so I kind of know what I'm doing. With the third component, I faced a problem where Chinese text displayd correctly on the component when it was written, but as soon as the user pressed enter or the text field otherwise lost focus, the characters changed to something completely wrong. This happened because I had both _MBCS and _UNICODE defined in the project and got fixed when I removed _MBCS. Now, with the fourth component I have the same problem but this time only _UNICODE is defined and I have no clue what causes this. In addition, the Chinese text displays correctly when it's first written to wordpad for example and then copy/pasted to component. Although this doesn't help since I have to use the component to send the Unicode text to another device and when we tested, the text is not sent correctly.
0
Comment
Question by:milzer
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9700260
That a component would spontaneously change what it is displaying seems very unlikely.  I suspect you have an event of some sort being triggered when the component looses focus that is changing the contents of what is displayed.  I think you need to look deeper into your code and see what's going on when the component looses focus.
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9702695
Do you have any DDV_ rountines defined within your Dialog's DoDataExchange function.

Sounds like a custom DDV_ routine may be the culprit.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 9702746

 are you using TCHARS or OLECHARS  or wchar_t's or BSTRS's ?
0
 

Author Comment

by:milzer
ID: 9713694
Actually, could these conversions be the source of this problem? This is done in killfocus, but it's called also when text is copy/pasted so I doubt that this causes the problem.

-----
CString str;
WCHAR temp_str[20];
BSTR bstrTempStr = NULL;

m_Edit.GetWindowText(bstrTempStr);
swprintf (temp_str, L"%s", bstrTempStr);
str.Format(_T("%ls"),temp_str);
-----

For jhance and _ys_: I didn't find any events or handlers that could cause this.
And for williamcampbell: There's TCHAR's, wchar's and BSTR's used in code, the component is done by some other guy about a year ago and I'm just a poor guy fixing it :)
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9716727
>> m_Edit.GetWindowText(bstrTempStr);
CWnd::GetWindowText doesn't even have a function that takes a BSTR as an argument.

If the ultimate goal is to store the text into a CString then:

CString str;
m_Edit.GetWindowText(str);

will simply do the desired job.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:williamcampbell
ID: 9716759

  my suggestion find all occurances of BSTR and replace them with _bstr_t BSTR's are usually used for COM stuff.

 otherwise as _ys_ suggests do all your string handling with CString


 
0
 

Author Comment

by:milzer
ID: 9721058
This is a COM component (as I said in my question already) so I have to use BSTR's. And the GetWindowText here doesn't accept CString parameter.

GroupListControl.cpp(1860) : error C2664: 'int __thiscall ATL::CWindow::GetWindowTextW(unsigned short ** )' : cannot convert parameter 1 from 'class WTL::CString' to 'unsigned short ** '

..I'm losing my hope with this :/
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9721184
> And the GetWindowText here doesn't accept CString parameter.
Apologies. I has assumed the use of MFC, as opposed to their ATL variants.

CString str;
TCHAR temp_str[20];

m_Edit.GetWindowText(&temp_str, 20);
str.Format(_T("%ls"),temp_str);

I've eliminated the intermediate BSTR as it was causing problems. This may not even be the cause of your problems, but at least the code is cleaner.
0
 

Author Comment

by:milzer
ID: 9730565
I still didn't found any reason for this, but when we finally got actual Chinese Windows and tested with it, the component worked just fine. With English Windows and Chinese localisations it didn't work.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 11975900
Submitted to PAQ with points refunded (125)

DarthMod
Community Support Moderator
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now