gpt4 book ai didi

c# - 将可变大小的 double 组从 C++ 返回到 C# - 一种更简单的方法?

转载 作者:太空狗 更新时间:2023-10-29 21:09:25 25 4
gpt4 key购买 nike

我有以下 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 数组中。有没有更好的方法来做到这一点而不涉及托管语言中的指针运算...

最佳答案

我觉得你可以用

Marshal.Copy( source, destination, 0, size );

关于c# - 将可变大小的 double 组从 C++ 返回到 C# - 一种更简单的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/442699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com