【发布时间】:2011-04-16 03:28:01
【问题描述】:
我想将一个整数数组从 C++ 编组到 C#。我有一个非托管 C++ dll,其中包含:
DLL_EXPORT int* fnwrapper_intarr()
{
int* test = new int[3];
test[0] = 1;
test[1] = 2;
test[2] = 3;
return test;
}
在标头 extern "C" DLL_EXPORT int* fnwrapper_intarr(); 中声明
然后我使用 pinvoke 将其编组为 C#:
[DllImport("wrapper_demo_d.dll")]
[return: MarshalAs(UnmanagedType.SafeArray)]
public static extern int[] fnwrapper_intarr();
我使用这样的功能:
int[] test = fnwrapper_intarr();
但是,在程序执行期间,我收到以下错误:SafeArray cannot be marshaled to this array type because it has either nonzero lower bounds or more than one dimension.
我应该使用什么数组类型?或者有没有更好的方法来编组数组或整数向量?
【问题讨论】:
-
如果你是 fnwrapper_intarr 的作者,有什么反对返回一个安全数组(或更好作为参数)的吗?