我用我自己的封送實現ICustomMarshaler與本機(非託管)DLL C函數工作。ICustomMarshaler :: MarshalNativeToManaged,接收來自本地的dll正確性「失去」的對象則返回
在功能MarshalNativeToManaged我看到從DLL側進入正確的結果。問題是,這MarshalNativeToManaged返回的對象不是「使用」。 (In,Out)參數調用函數中的對象不會更改。
(它看起來正是在previosly這裏討論同樣的問題,「C#:使用的PInvoke調用之後不包含數據的自定義編組對象」) 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);
某處主要我這樣稱呼它THI s:
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不是「其他一些周圍的方式」來處理吧:)
非常感謝你的幫助。這是處理它的方法之一。我想我知道這裏發生了什麼,我已經回答了我自己的問題。當(如果)你有一些時間,請看看並添加一些評論/更正。 – AlexZJP
嗯,不,全局變量*不是*處理它的方式。 –
它不是全局的,它是類ZMarshal的成員變量。順便說一句,我們如何控制我們使用的封送處理器類的哪個實例? – AlexZJP