我用我自己的元帅执行ICustom Marshaller 来操作本地(未管理的) dll C 功能。
函数“元元帅ToManage”中,我确实看到来自涂层的正确结果。问题在于“元帅”管理回报的物体没有“使用”。调用参数( In, Out)中的物体没有被修改。
(It looks like it is exactly the same problem that was previosly discussed here "C#: Object with custom marshaller not containing data after PInvoke call") C#: Object with custom marshaller not containing data after PInvoke call
简单类 :
[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);
在《元帅管理》中,我确实看到 调试功能的正确结果。
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
}
What am I doing wrong here? Just a quick note: I do want to know how to handle it using CustomMarshaler not "some other way around" :)