Discussion:
loadstring fails in MBCS encoding
(too old to reply)
news
2005-11-09 15:47:40 UTC
Permalink
Hi,

Implementation of CString::LoadString has changed from VC6 to VC7 and I
can't load MBCS resources correctly any more.
When I translate the application to Russian or Chinese languages, dialogs
and menus and all strings defined directly in the resource file are well
displayed but
the problem is when I have to load dynamicaly some resources like this :

CString s;
s.LoadString (MY_ID_RESOURCE);
m_edit.SetWindowText(s);

Then, the string will be "????????" instead of the rigth one.

Debugging, I have found that in VC7 LoadString implementation makes a
conversion with a call to ConvertToBaseType method.

I have found two solutions :

the first one is to use TCHAR instead of CString :

TCHAR buf[255];
LoadString(NULL, MY_ID_RESOURCE, buf, 255);
m_edit.SetWindowText(buf);

the second one is to change properties of the project by changing Character
Set from MBCS to Unicode.

Unfortunatly, neither of the two solutions are possible.

The first one cannot be used because there are also dynamic loads in mfc
code (like for the title of the main frame).

The second one makes two much changes in the code of the application which
is two large.

Does any body has an idea ?
Thanks in advance.
Selena
Tom Serface
2005-11-09 16:35:36 UTC
Permalink
Are you compiled as ANSI or UNICODE? If the later, you could try CStringA()
to see if that works. CString will become either CStringW or CStringA
depending on the compile options.

Tom
Post by news
Hi,
Implementation of CString::LoadString has changed from VC6 to VC7 and I
can't load MBCS resources correctly any more.
When I translate the application to Russian or Chinese languages, dialogs
and menus and all strings defined directly in the resource file are well
displayed but
CString s;
s.LoadString (MY_ID_RESOURCE);
m_edit.SetWindowText(s);
Then, the string will be "????????" instead of the rigth one.
Debugging, I have found that in VC7 LoadString implementation makes a
conversion with a call to ConvertToBaseType method.
TCHAR buf[255];
LoadString(NULL, MY_ID_RESOURCE, buf, 255);
m_edit.SetWindowText(buf);
the second one is to change properties of the project by changing
Character Set from MBCS to Unicode.
Unfortunatly, neither of the two solutions are possible.
The first one cannot be used because there are also dynamic loads in mfc
code (like for the title of the main frame).
The second one makes two much changes in the code of the application which
is two large.
Does any body has an idea ?
Thanks in advance.
Selena
Heinz Ozwirk
2005-11-10 10:40:12 UTC
Permalink
Post by news
Hi,
Implementation of CString::LoadString has changed from VC6 to VC7 and I
can't load MBCS resources correctly any more.
When I translate the application to Russian or Chinese languages, dialogs
and menus and all strings defined directly in the resource file are well
displayed but
CString s;
s.LoadString (MY_ID_RESOURCE);
m_edit.SetWindowText(s);
Then, the string will be "????????" instead of the rigth one.
Debugging, I have found that in VC7 LoadString implementation makes a
conversion with a call to ConvertToBaseType method.
Usually it is reasonable for a MBCS app to convert a unicode string (which
comes from the resource) into a MBCS string in the thread's current
codepage. All characters, that can not be represented in that codepage will
be translated into similiar characters (usually by removing diacritic marks)
or, if there is no similiar character in the target codepage, into some bad
chaacter code (usually '?'). What else could be done? In a MBCS app, unicode
must be translated into MBCS and CString::LoadString does the obvious - try
to convert it as good as possible. After all, the standard font used in
dialogs can only display characters from the current codepage.

To display strings with characters from another codepage, you have to do two
things:

1. You have to tell CString::LoadString to use another codepage. This can be
done by calling SetThreadLocale before calling CString::LoadString. Make
sure to revert to the system's default codepage as soon as possible,
otherwise other parts of the program might not work properly.

2. You have to select another font to be used in dialogs. Perhaps you
already did that, or I couldn't explain why ::LoadString works or why it did
work with VC6.
Post by news
TCHAR buf[255];
LoadString(NULL, MY_ID_RESOURCE, buf, 255);
m_edit.SetWindowText(buf);
I don't know exaclty how ::LoadString works. I can only guess that it uses
the locale specified in the resource to translate the unicode string into
MBCS. If that would be true, any foreign characters should be shown as some
(wrong) characters from the current codepage (unless you select another
font).
Post by news
the second one is to change properties of the project by changing
Character Set from MBCS to Unicode.
That would most likely be the best solution. However, if the app is not
already unicode aware, whoever wrote the initial app did a poor job. If you
cannot fix this, the only solution I can think of, is modifying the thread's
locale, at least temporary. But keep in mind, the problem will probably go
away when the app runs on a machine configured for the target language.

HTH
Heinz
Olivier
2005-11-18 14:42:55 UTC
Permalink
Did you try to force the locale (language) with setlocale ?

setlocale( LC_ALL, "china");

char *setlocale(
int category,
const char *locale
);
Post by news
Hi,
Implementation of CString::LoadString has changed from VC6 to VC7 and I
can't load MBCS resources correctly any more.
When I translate the application to Russian or Chinese languages, dialogs
and menus and all strings defined directly in the resource file are well
displayed but
CString s;
s.LoadString (MY_ID_RESOURCE);
m_edit.SetWindowText(s);
Then, the string will be "????????" instead of the rigth one.
Debugging, I have found that in VC7 LoadString implementation makes a
conversion with a call to ConvertToBaseType method.
TCHAR buf[255];
LoadString(NULL, MY_ID_RESOURCE, buf, 255);
m_edit.SetWindowText(buf);
the second one is to change properties of the project by changing
Character Set from MBCS to Unicode.
Unfortunatly, neither of the two solutions are possible.
The first one cannot be used because there are also dynamic loads in mfc
code (like for the title of the main frame).
The second one makes two much changes in the code of the application which
is two large.
Does any body has an idea ?
Thanks in advance.
Selena
Loading...