我完全不知道如何使用数组参数调用本机 dll 中的函数。
Example:
该函数在 C# 项目中定义为:
[DllImport("Project2.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern void modifyArray([MarshalAs(UnmanagedType.LPArray)] int[] x, int len);
函数调用是:
modifyArray(arr, 3)
其中 arr = {4,5,6}
原生C++函数定义如下:
extern "C" _declspec(dllexport) void modifyArray(int* x,int len)
{
int arr[] = {1,2,3};
x = arr;
}
为什么在C#项目中,函数调用后数组没有指向新数组?它仍然是{4,5,6}。
我尝试过但失败了
[DllImport("Project2.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern void modifyArray([In,Out] int[] x, int len);
每当我尝试使用指针修改传递给这些函数的参数时,此 pinvoke 都会失败。否则,我已成功传递本机 dll 排序函数的 ref 数组参数,其中新创建的类型没有指针更改。