C++封装函数,数组作为参数输出,C#调用

C++代码

extern "C" _declspec(dllexport) void _cdecl SunmDoubles2(const double* arr, int length, double* arr1)
{
    double sum = 0.0;
    for (int i = 0; i < length; i++)
    {
        arr1 [i] = arr[i]*2;
    }
    /*return sum;*/
}

 

C#代码

 [DllImport("ConsoleApplication1.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
 public static extern void SunmDoubles2(ref double arr, int length,out  double arr1);
 double[] arr = new double[] { 1.1, 2.2, 3.3 };
 double[] ar1r = new double[3] ;
 SunmDoubles2(ref arr[0], 3, out ar1r[0]);

 或者

 [DllImport("ConsoleApplication1.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
 public static extern void SunmDoubles2(ref double arr, int length,  double[] arr1);
   double[] arr = new double[] { 1.1, 2.2, 3.3 };
   double[] ar1r = new double[3] ;
   SunmDoubles2(ref arr[0], 3,  ar1r);

 

posted @ 2026-05-30 15:10  家煜宝宝  阅读(6)  评论(0)    收藏  举报