加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Marshal的[In] [Out]属性

发布时间:2020-12-15 04:19:49 所属栏目:百科 来源:网络整理
导读:我在我的C#代码中调用了一个非托管函数. 该功能的声明如下: int myFun(unsigned char* inputBuffer,unsigned char* outputBuffer); 我使用这个函数如下: [DllImport("myDLL.dll",CallingConvention = CallingConvention.Cdecl)]private static extern int
我在我的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正在使用其自己的默认编组行为.
我使用了byte []因为它与C#等价于unsigned char *.

当我用char []替换byte []时,CLR将遵循我的In-Out属性.

我非常感谢你的帮助.
有关更多信息,请阅读此msdn page.

解决方法

从 documentation开始,我强调:

As an optimization,arrays of blittable types and classes that contain only blittable members are pinned instead of copied during marshaling. These types can appear to be marshaled as In/Out parameters when the caller and callee are in the same apartment. However,these types are actually marshaled as In parameters,and you must apply the InAttribute and OutAttribute attributes if you want to marshal the argument as an In/Out parameter.

由于字节是blittable,因此您的字节数组被固定而不是复制,因此编组为In / Out.

When I substitute byte[] with char[] the CLR will follow my In-Out attributes.

C#类型的char是not blittable.不固定char数组,因此[In] char []编组作为In参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读