ICustomMarshalerを実装する独自のマーシャラーを使用して、ネイティブ(管理されていない)dllC関数を操作しています。
関数MarshalNativeToManagedで、dll側からの正しい結果が表示されます。問題は、MarshalNativeToManagedが返すオブジェクトが「使用」されていないことです。(In、Out)パラメーターを指定して呼び出している関数のオブジェクトは変更されません。
(これは、前にここで説明した「C#:PInvoke呼び出し後にデータを含まないカスタムマーシャラーを持つオブジェクト」とまったく同じ問題のようです) C#:PInvoke呼び出し後にデータを含まないカスタムマーシャラーを持つオブジェクト
単純なクラス:
[StructLayout(LayoutKind.Sequential)]
class CMA {
public int a;
char b;
public char get_b() { return b; }
}
関数のシグネチャは次のようになります。
[DllImport("first.dll", EntryPoint = "hack")]
public static extern int hack([In, Out, MarshalAs(UnmanagedType.CustomMarshaler,
MarshalTypeRef=typeof(ZMarshal))] CMA cma);
メインのどこかで私はそれをこのように呼んでいます:
int retcode = hack(cma);
MarshalNativeToManagedで、dll関数呼び出しからの正しい結果が表示されます。
public object MarshalNativeToManaged(IntPtr pNativeData)
{
// everything is fine with pNativeData;
// but let us even say I ignore it
// and return the object in the following way:
CMA cma = new CMA();
cma.a = 999;
return cma; // this will be lost. I mean cma object in the main will not be changed
}
私はここで何が間違っているのですか?簡単なメモ:「他の方法」ではなく、CustomMarshalerを使用して処理する方法を知りたいです:)