次の方法で共有


方法: out パラメーターを指定する

この例では、関数パラメーターが out パラメーターであることを指定する方法、および C# プログラムからその関数を呼び出す方法を示します。

out パラメーターは、Visual C++ 内で OutAttribute を使用して指定されます。

使用例

この例の最初の部分は、out パラメーターを使用する関数を含む型を持つ Visual C++DLL です。

// cpp_out_param.cpp
// compile with: /LD /clr:safe
using namespace System;
public value struct TestStruct {
   static void Test([Runtime::InteropServices::Out] String^ %s) {
      s = "a string";
   }
};

これは、前の例で作成された Visual C++ コンポーネントを利用する C# クライアントです。

// cpp_out_param_2.cs
// compile with: /reference:cpp_out_param.dll
using System;
class TestClass {
   public static void Main() {
      String t;
      TestStruct.Test(out t);
      System.Console.WriteLine(t);
   }
}
  

参照

関連項目

C++ Interop (暗黙の PInvoke) の使用