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

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

问题描述

我有以下的C ++方式:

I have the following C++ method :

__ declspec(dllexport)的无效​​__stdcall getDoubles为(int *计数,双**值);
该方法分配并填补了双阵列,并设置*算到数组的大小。

__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的工作的唯一办法是:

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数组中结束。
有没有更好的办法TI在托管语言做到这一点并不invloving指针运算...

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天全站免登陆