从C ++返回一个可变大小的双精度数组到C# - 一个更简单的方法? [英] Returning a variable sized array of doubles from C++ to C# - a simpler way?

查看:246
本文介绍了从C ++返回一个可变大小的双精度数组到C# - 一个更简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C ++方法:

I have the following C++ method :

__ declspec(dllexport)void __stdcall getDoubles(int * count,double ** values)
该方法分配并填充一个double数组,并将* count设置为数组的大小。

__declspec(dllexport) void __stdcall getDoubles(int *count, double **values); the method allocates and fills an array of double and sets *count to the size of the array.

我设法通过pinvoke is:

The only way i managed to get this to work via pinvoke is :

[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数组中结束
有一个更好的方法,这不会在托管语言中调用指针运算...

the values end up in the dvs array. Is there a better way ti do this not invloving pointer arithmetic in a managed language...

推荐答案

我想你可以使用

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

这篇关于从C ++返回一个可变大小的双精度数组到C# - 一个更简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆