tags:

views:

1010

answers:

4

How do I get an old VC++ 6.0 MFC program to read and display UTF8 in a TextBox or MessageBox? Preferably without breaking any of the file reading and displaying that is currently written in there (fairly substantial).

I read a line into CString strStr, then used this code:

int nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,strStr,1024,0,0);
wchar_t * pWCMessage = new wchar_t[ nLengthNeeded ];

MultiByteToWideChar(CP_UTF8,0,strStr,1024,pWCMessage,nLengthNeeded);

nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,"Error Title",50,0,0);
wchar_t * pWCTitle = new wchar_t[ nLengthNeeded ];
MultiByteToWideChar(CP_UTF8,0,"Error Title",50,pWCTitle,nLengthNeeded);

MessageBoxW(NULL,pWCMessage,pWCTitle,MB_ICONINFORMATION);

Still not sure how I would get it into a textbox, but it turns out I don't need to do that anyway.

A: 

take a look at:

widechartomultibyte and for more general info

when/if you run into trouble, make sure you post your code. It's been a while since I did that and I remember it being a bit tricky.

cbrulak
+2  A: 

I feel like this won't be helpful, but it's a starting point... I'm assuming it doesn't 'just work', and I don't think you want to try to screw around with wacky code pages that may or may not get you what you want.

How about just using MultiByteToWideChar(CP_UTF8, ...) to convert it to utf16 and then calling the W versions of those functions (or defining UNICODE/_UNICODE for the project).

I know that will work for MessageBox, and I can't imagine the text box doesn't have unicode support.

If you need to get the output back to UTF8 - just use WideCharToMultiByte().

Joe
Calling the W functions on a Textbox likely won't work, if you created it with CreateWindowA. You've got to be consistent.
MSalters
A: 

Convert the utf8 string first to a wide string with the API MultiByteToWideChar, passing CP_UTF8 for the codepage parameter. If your application is compiled with _UNICODE defined, you can now pass the wide string to your textbox.

If your application however is compiled as an MBCS application, you have to convert the wide string back to MBCS with WideCharToMultiByte, passing CP_ACP as the codepage parameter.

Stefan
A: 

Is your app Unicode. If yes, fallback to Stefan's answer:

MyDisplayableUtf16String = MultiByteToWideChar(CP_UTF8, MyUtf8String,...)

I'll assume it's not as simple: Your app is ANSI. First of all, you need to convert the string to UTF16 as shown above. Then there is a bottleneck: Does your UTF8 string contain characters ouside the current system codepage (aka "Language for non-Unicode applications")? If not, convert the UTF16 string to the system locale using

MyGoodOldAnsiString = WideCharToMultiByte(CP_ACP,MyUtf16String,...)

(or use one of the ATL/MFC macros, such as W2A(MyUtf16String))

and you're done.

Else the string can't be converted to ANSI, which means you'll have a... ahem... hard time trying to display it in an ANSI textbox or message box.

As Joe pointed out, you can display the text in a mesage box using

MessageBoxW(...,MyDisplayableUtf16String,...)

The trailing W denotes the Unicode (UTF16) version of the API.

Displaying the string in a textbox will remain difficult though: You'd need to programmaticaly create the text box as a Unicode Window using CreateWindowExW(). Which I believe won't work if the parent window of the textbox (dialog,frame?) is not a Unicode window.

Serge - appTranslator