c# – Marshal的[In] [Out]属性
我在我的C#代码中调用了一个非托管函数.
该功能的声明如下: int myFun(unsigned char* inputBuffer,unsigned char* &outputBuffer); 我使用这个函数如下: [DllImport("myDLL.dll",CallingConvention = CallingConvention.Cdecl)] private static extern int myFun([In] byte[] inputBuffer,out IntPtr outputBuffer); byte[] a = Encoding.ASCII.GetBytes("sampletext!"); IntPtr b; res = myFun(a,out b); 虽然我提到了byte [] inputBuffer的[InAttribute],但该函数仍然改变了a的值.似乎CLR正在使用其自己的默认编组行为. 当我用char []替换byte []时,CLR将遵循我的In-Out属性. 我非常感谢你的帮助. 解决方法
从
documentation开始,我强调:
由于字节是blittable,因此您的字节数组被固定而不是复制,因此编组为In / Out.
C#类型的char是not blittable.不固定char数组,因此[In] char []编组作为In参数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |