Converting Multiple Byte Characters between C++/CLI and Notes C API

To manipulate the multiple byte characters like Japanese, Korean, Chinese with Notes C API, you will need to convert Native characters Code to LMBCS.

As per C++/CLI supports UNICODE as default, I recommend to use UNICODE for manipulating the String values on C++/CLI.

Translate from UNICODE (managed code) to LMBCS

Here I wrote how to convert UNICODE characters to LMBCS so that C API functions can understand them.

Step1. First of all, Marshal String to UNICODE String Pointer from Managed code. Marshal::StringToHGlobalUni() function  allocates the string data to global memory, so you will need to release the data after all.

String^ idfile = gcnew String(“ほげほげ.id”);
// Convert String to UNICODE String Pointer ( memo:StringToCoTaskMemAuto )
System::IntPtr ptrIdfile = System::Runtime::InteropServices::Marshal::StringToHGlobalUni( idfile );

Step2. Translate string from UNICODE to LMBCS format with OS_TRANSLATE_UNICODE_TO_LMBCS as 1st parameter of OSTranslate.

WORD    idfile_len = 2*(wcslen(static_cast<const wchar_t*>(ptrIdfile.ToPointer())));
char    idfile_lmbcs[MAXBUFFER+1];
OSTranslate(OS_TRANSLATE_UNICODE_TO_LMBCS, (char*)ptrIdfile.ToPointer(), idfile_len, idfile_lmbcs, MAXBUFFER );

Now you can pass LMBCS characters which is contained in idfile_lmbcs to Notes C API functions.

After using the string, don’t forget to free the pointer which is allocated by Marshal::StringToHGlobalUni().

System::Runtime::InteropServices::Marshal::FreeHGlobal( ptrIdfile );


› Translate from LMBCS to UNICODE (managed code)

For example, To get the multiple byte characters from notes documents, you also need to translate string from LMBCS to UNICODE. Below is the steps.

Step1. Get the value as Char format.

char item_value[MAXTEXTBUFFER+1];
// for example, calling NSFItemConvertValueToText() to get the Text from item value.
text_len = NSFItemConvertValueToText( item_type,
value_block,
item_len,
item_value,
buffer_len,
0);

Step2. Translate text string from LMBCS to UNICODE. In this case you need to use OS_TRANSLATE_LMBCS_TO_UNICODE as 1st parameter of OSTranslate().

char    item_value_lmbcs[MAXTEXTBUFFER+1];
OSTranslate(OS_TRANSLATE_LMBCS_TO_UNICODE, item_value, text_len, item_value_lmbcs, MAXTEXTBUFFER );

Step3.Get string as managed code by converting with following code.

String^ itemValue = System::Runtime::InteropServices::Marshal::PtrToStringUni( (IntPtr)item_value_lmbcs );

Tagged with: , , , ,

Leave a Reply

Your email address will not be published.

*




Enter Captcha Here :