如何从C ++ DLL函数返回double型数组并在C#中接收该double型数组

时间:2019-01-27 09:11:04

标签: c# c++

C ++ DLL函数获取一个double数组,对其进行处理,然后返回double数组。 C#代码接收该数组并进行打印。但出现异常Exception thrown by C# code

如果从C ++ DLL函数而不是数组返回单个变量,则代码可以正常工作。这是我的C ++ DLL代码:

Header.h

extern "C" __declspec(dllexport) double*  __cdecl epics(double c[]);

Source.cpp

#include "stdafx.h"
#include "Header.h"

double* epics(double c[])
{
    double a=0;
    for (int i = 0; i < 3; i++)
    {
        c[i] = c[i]*2;
    }
    return c;
}

我的C#代码

[DllImport(@"MyFirstDLL.dll", CallingConvention = CallingConvention.Cdecl,
SetLastError = true)]
public static extern double[] epics(double[] arr);

private void Mimic_Loaded(object sender, RoutedEventArgs e)
{
   double[] b = new double[] { 1, 2, 4.2f };
   double[] theResult = epics(b);
   foreach (var item in theResult)
   {
        MessageBox.Show(item.ToString());
    }          
  }

0 个答案:

没有答案