Using MBCS in MFC application to display Russian characters

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="color:#222222; font-family:Droid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left (I am using Visual Studio 2008 with VC++)<br style="font-size:14px; font-family:Droid Serif,Georgia,Times New Roman,serif; color:#222222; line-height:23px; text-align:left
<span style="color:#222222; font-family:Droid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left My MFC application reads a text file and uses its contents to alter the text on the buttons of the GUI. The application
uses MBCS. Recently, I was asked to add support for Russian text. I can use Notepad to create the text file I need (in Unicode), but when I convert the Unicode to MBCS, I get gibberish. Here is a code sample:
<pre class="prettyprint #include <locale.h>
#define SETLOCALE(locale)
if ( !setlocale(LC_ALL, locale) )
{
AfxMessageBox("setlocale failed for " locale ", this locale is probably not installed on your system");
}

CStringList* ReadFile(const CString& strPath)
{
CStringList* pList = new CStringList();

FILE* pFile;
errno_t error = _tfopen_s(&pFile, strPath, "r");
if ( error == 0 )
{
// See if the file is Unicode
char buf[10];
_fgetts(buf, 3, pFile);
if ( (buf[0] == -1) && (buf[1] == -2) )
{
// File is unicode. Have to close and reopen the file, then strip off the BOM
fclose(pFile);
error = _tfopen_s(&pFile, strPath, "rb");
if ( error == 0 )
{
wchar_t buf[1000];
fgetws(buf, 2, pFile);
TCHAR mbsBuf[1000];
SETLOCALE("Russian");
while ( fgetws(buf, 999, pFile) != NULL )
{
size_t dummy;
wcstombs_s(&dummy, mbsBuf, 1000, buf, 1000);
CString strLine(mbsBuf);
strLine.Trim();
AfxMessageBox(strLine);
pList->AddTail(strLine);
}
}
}
fclose(pFile);
}

return pList;
}[/code]
<br/>
<span style="color:#222222; font-family:Droid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left Until I added the SETLOCALE("Russian"), the wcstombs() function failed with a EILSEQ error. Now, all I get are characters
that are in the extended ASCII range.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px I tried replacing wcstombs() with WideCharToMultiByte() with no success. I tried several code pages.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Can anyone help?
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Thanks!
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px JAB
<br/>


View the full article
 
Back
Top