C++CLIとNotes C APIの間でのマルチバイト文字の変換

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: , , , ,

C++/CLIでNotes C APIを使用する設定

Notes C API を C++/CLI から使う方法を書いたものがインターネット上に見当たらなかったので書いてみました。

以下の設定はVisual Studio 2005 Standard Edition (英語バージョン)を使っています。

  1. New Projectからプロジェクトを作成したあと、[Project] – [Properties]からプロパティを開きます。
  2. 左のナビゲーションから [Configuration Properties] – [General] を開く。Dynamic Library(.dll) Common Language Runtime Support (/clr) を設定
    Configuration Properties - General

    Configuration Properties - General

  3. プロパティから [C/C++] – [General]を開きます。[Additional Include Directories]に Notes C API include folderを追加。

    C/C++ - General

    C/C++ - General

  4. [C/C++] – [Peprocessor]を開きます。[Preprocessor Definitions]に W32;WIN32 を追加。

    C/C++ - Preprocessor

    C/C++ - Preprocessor

  5. [C/C++] – [Code Generation]を開きます。[Runtime Library]からMulti-threaded DLL (/MD)を選択。

    C/C++ - Code Generation

    C/C++ - Code Generation

  6. [Linker] – [General]を開きます。[Additional Include Directories]にC Notes API Libraryを追加。

    Linker - General

    Linker - General

  7. [Linker] – [Input]を開きます。[Additional Dependencies]にnotes.lib を追加。
    Linker - Input

    Linker - Input

    必要であればnotes.libを相対パスで指定します。(例)notesapilibmswin32notes.lib

以上です。

Notes C APIを快適なC++/CLIのプログラミングでラッピングしてしまうことで、他のプログラム(C#やJava)とのインテグレーションが容易になると思います。

Top