views:

192

answers:

1

I'm currently working on a project that is written in C++ and uses True OLE DBGrids with a MS Access backend. This all works well until now we need to be able to convert our GUI to display Arabic characters. The DBGrids do not use Unicode so I need a way to display the characters from the database without using Unicode. Currently I have set the Regional language settings to Arabic and when I did this I can force the grids to display the Arabic characters but I can't do it through code. I need to be able to change between English and Arabic at run time. I can pull the data out of the database and then convert it to non-Unicode using the following code:

_bstr_t tmp(vHolder.bstrVal, FALSE);   //wrap the BSTR
CString Caption(static_cast<const char*>(tmp));  //convert it
RetCaption = Caption;

With this I can then post the Arabic to an AfxMessageBox and it displays correctly but I can't seem to get the grids to accept the non-Unicode characters and display them correctly.

Any ideas?

+1  A: 
CString Caption(static_cast<const char*>(tmp));  //convert it

That doesn't convert it. It produces a single character if tmp contains English text. And a common saying from the land of Jibber if it contains Arabic text. Converting from UTF-16, as used by OLE Automation and the DBGrid you are using to 8-bit characters cannot be done with a cast, it requires a conversion function. Like WideCharToMultiByte or OLE2A.

Such a conversion will only reproduce legible text when the code page for the thread matches the character set used in the string. Which, if the grid shows Arabic and either your thread or the code page you convert to is English produces nothing but a bunch of question marks.

If you don't want to Unicode-enable your code then you can't switch between character sets on the fly. The operating system you are running this code on has supported it for the past 17 years.

Hans Passant