(我知道这可能是重复的,但我不理解其他线程)
我正在使用C#,我有一个需要int数组(或int数组指针)作为参数的第三方dll
。我如何在C#和C/C++之间传输int数组?函数声明如下:
// reads/writes int values from/into the array
__declspec(dllimport) void __stdcall ReadStuff(int id, int* buffer);
在C语言中,
int*
代表指针,那么我现在有些困惑,我是否需要使用IntPtr
或者可以使用int[]
(更好)?我认为这样是可以的:[DllImport(dllName)]
static extern void ReadStuff(int id, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)] ref int[] buffer);
// call
int[] array = new int[12];
ReadStuff(1, ref array);
那会行吗?或者我需要如何在安全代码中声明这个函数?
int* data = &array[0]
可以缩写为int* data = array
(就像在 C/C++ 中一样)。 - xanatos