Solved

Unicode characters act weirdly

Posted on 2003-11-06
12
264 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Smart Camera scanning and reading information 3 99
wait notify demo infinite loop 3 95
post4 challenge 28 92
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 250
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

912 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

23 Experts available now in Live!

Get 1:1 Help Now