P/Invoke [In, Out] 属性对于封送处理数组是可选的吗? [英] Are P/Invoke [In, Out] attributes optional for marshaling arrays?

查看:25
本文介绍了P/Invoke [In, Out] 属性对于封送处理数组是可选的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个带有纯 C 接口的本机函数,如下所示,从本机 DLL 导出:

Assume that there is a native function with a pure-C interface like the following one, exported from a native DLL:

// NativeDll.cpp

extern "C" void __stdcall FillArray(
    int fillValue, 
    int count, 
    int* data)
{
    // Assume parameters are OK...

    // Fill the array
    for (int i = 0; i < count; i++)
    {
        data[i] = fillValue;
    }
}

以下 P/Invoke 工作正常(使用 VS2010 SP1 测试):

The following P/Invoke works fine (tested with VS2010 SP1):

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    [In, Out] int[] data
);

还有这个P/Invoke,同上,但没有[In, Out]属性:

as well as this P/Invoke, same as above, but without the [In, Out] attributes:

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    int[] data
);

那么,那些[In, Out] 属性可选 是否用于编组数组?如果有的话,他们的目的是什么?可以在我们的 P/Invoke 声明中省略它们吗?

So, are those [In, Out] attributes optional for marshaling arrays? What is their purpose, if any? Is it OK to omit them in our P/Invoke declarations?

推荐答案

不,它们不是完全可选的.它只是偶然地起作用.然而,这是一个非常常见的事故.它之所以有效,是因为该数组实际上并未被编组.pinvoke marshaller 发现 C# 数组已经与本机数组兼容,因此跳过创建它的副本的步骤.它只是固定数组并将指针传递给本机代码.

No, they are not exactly optional. It just happens to work by accident. It is however a very common accident. It works because the array doesn't actually gets marshaled. The pinvoke marshaller sees that the C# array is already compatible with the native array so skips the step to create a copy of it. It merely pins the array and passes the pointer to the native code.

这当然是非常有效的,你不可避免地会得到结果,因为原生代码是直接写入数组元素的.所以 [In] 和 [Out] 属性都不重要.

This is of course very efficient and you will inevitably get the results back because the native code is directly writing the array elements. So neither the [In] nor the [Out] attributes matter.

如果数组元素类型不是那么简单,它会变得更加模糊.识别不是 blittable 或其布局在编组后不匹配的结构或类类型的元素类型并不容易,因此 pinvoke 编组器 必须 制作数组的副本.尤其是布局不兼容可能非常难以识别,因为托管布局是不可发现的.并且可以根据所使用的抖动而改变.It may work in x86 but not x64 for example, pretty nasty when AnyCPU is selected.让它将修改后的副本复制回 C# 数组确实需要 [Out].

It gets much murkier if the array element type isn't that simple. It isn't that easy to identify an element type that's a struct or class type that isn't blittable or whose layout doesn't match after marshaling so the pinvoke marshaller has to make a copy of the array. Especially the layout incompatibility can be very hard to identify because the managed layout is undiscoverable. And can change depending on the jitter that is used. It may work in x86 but not x64 for example, pretty nasty when AnyCPU is selected. Getting it to copy the modified copy back to the C# array does require [Out].

不知道该建议什么,除了没有人因为在声明中明确而被解雇.当数组元素类型不简单时,也许你应该总是明确的,这样你就不会发生意外.

Not sure what to advice, other than that nobody ever got fired for being explicit in their declarations. Perhaps you should always be explicit when the array element type isn't simple so you'll never have an accident.

这篇关于P/Invoke [In, Out] 属性对于封送处理数组是可选的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆