【发布时间】:2010-10-01 08:36:13
【问题描述】:
我有以下 C++ 方法:
__declspec(dllexport) void __stdcall getDoubles(int *count, double **values); 该方法分配并填充一个 double 数组,并将 *count 设置为数组的大小。
我设法通过 pinvoke 让它工作的唯一方法是:
[System.Runtime.InteropServices.DllImportAttribute("xx.dll")]
public static extern void getDoubles(ref int count, ref System.IntPtr values);
用法是:
int count = 0;
IntPtr doubles = new IntPtr();
Nappy.getDoubles(ref count, ref doubles);
double[] dvs = new double[count];
for(int i = 0;i < count;++{
dvs[i] = (double)Marshal.PtrToStructure(doubles, typeof(System.Double));
doubles = new IntPtr(doubles.ToInt64()+Marshal.SizeOf(typeof(System.Double)));
}
这些值最终出现在 dvs 数组中。 有没有更好的方法来做到这一点而不涉及托管语言中的指针运算......
【问题讨论】:
-
描述不完整——它是如何分配内存的,又是如何返回给那个分配器的?