「C++の応用 - C Sharp DLLの使用」の版間の差分

ナビゲーションに移動 検索に移動
488行目: 488行目:
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
  // C#ライブラリがタプル型の戻り値を返す場合
  // C#ライブラリがタプル型の戻り値を返す場合
struct TupleResult {
    int        Field1;  // int型
    MonoString* Field2;  // string型
};
  MonoObject *pResult = mono_runtime_invoke(method, instance, params, &exc);
  MonoObject *pResult = mono_runtime_invoke(method, instance, params, &exc);
   
   
498行目: 503行目:
  {
  {
     // タプル型から個々の値を取得
     // タプル型から個々の値を取得
     MonoArray *pResultArray = reinterpret_cast<MonoArray*>(pResult);
     TupleResult *tupleResult = static_cast<TupleResult*>(mono_object_unbox(pResult));
     int        intValue = tupleResult->Field1;
     TupleResult tupleResult;
     std::string strValue = mono_string_to_utf8(tupleResult->Field2);
    tupleResult.intValue   = *reinterpret_cast<int*>(mono_array_addr(pResultArray, int, 0));
     tupleResult.stringValue = mono_string_to_utf8(reinterpret_cast<MonoString*>(mono_array_addr(pResultArray, MonoString*, 1)));
   
   
     // 結果を出力
     // 結果を出力
     std::cout << "Method result: " << tupleResult.intValue << ", " << tupleResult.stringValue << std::endl;
     std::cout << "Method result: " << intValue << ", " << strValue << std::endl;  
    // メモリの解放
    mono_free(tupleResult.stringValue);
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>

案内メニュー