如何通过P / Invoke在C ++中填充C#结构数组

时间:2016-10-09 23:48:43

标签: c# c++ struct pinvoke

我使用C ++执行一些计算并将填充的数据结构数组返回给C#。我的研究使我得出结论,最好的方法是首先在C#中实例化结构,然后将它们发送到C ++进行填充。如果这不是最好的方法,请告诉我。

所以我试图通过P / Invoke调用在C ++中填充一系列C#结构。我已经在C ++中成功填充了一个C#结构,但我正在努力使用该数组。以下是单个和(尝试)多个群体的C ++结构和逻辑:

struct Prediction
{
    int Combination[6] = {};
    double Value;
};

extern "C"
{

    ///<summary>Populate a pointer to a test prediction struct with data</summary>
    __declspec(dllexport) void PopulateTestPrediction(OUT Prediction* pPrediction)
    {
        (*pPrediction).Value = 42;
        for (size_t i = 0; i < 6; i++)
        {
            (*pPrediction).Combination[i] = i;
        }
    }

    ///<summary>Populate a pointer to a test prediction struct with data</summary>
    __declspec(dllexport) void PopulateTestPredictions(OUT Prediction** pPredictions, int predictionCount)
    {
        for (size_t i = 0; i < predictionCount; i++)
        {
            Prediction* pPrediction = pPredictions[i];
            (*pPrediction).Value = i;
            for (size_t j = 0; j < 6; j++)
            {
                (*pPrediction).Combination[j] = j;
            }
        }
    }

}

以下是我用来调用C ++的C#代码:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;

namespace Estimator.Predictors
{

    [StructLayout(LayoutKind.Sequential)]
    public struct Prediction
    {

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public int[] Combination;

        /// <summary></summary>
        public double Value;

    }

    public class PredictorManager
    {

        [SuppressUnmanagedCodeSecurity]
        [DllImport("NativeEstimator.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void  PopulateTestPrediction(ref Prediction prediction);

        [SuppressUnmanagedCodeSecurity]
        [DllImport("NativeEstimator.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void PopulateTestPredictions(ref Prediction[] predictions, int length);

        public static void TestPredictionPopulate()
        {
            var prediction = new Prediction {Combination = new int[6]};
            PopulateTestPrediction(ref prediction);
            Debug.WriteLine($"SizeOf: {Marshal.SizeOf(prediction)}");
            Debug.WriteLine($"Expected: {42.0}, Actual: {prediction.Value}");
            for (int i = 0; i < 6; i++) Debug.WriteLine($"Expected: {i}, Actual: {prediction.Combination[i]}"); ;
        }

        public static void TestPredictionsPopulate()
        {
            const int PREDICTION_COUNT = 4;
            Prediction[] predictions = new Prediction[PREDICTION_COUNT];
            for (int j = 0; j < PREDICTION_COUNT; j++)
            {
                predictions[j].Combination = new int[6];
            }
            PopulateTestPredictions(ref predictions, PREDICTION_COUNT);
            for (int j = 0; j < PREDICTION_COUNT; j++)
            {
                Debug.WriteLine($"SizeOf: {Marshal.SizeOf(predictions[j])}");
                Debug.WriteLine($"Expected: {42.0}, Actual: {predictions[j].Value}");
                for (int i = 0; i < 6; i++) Debug.WriteLine($"Expected: {i}, Actual: {predictions[j].Combination[i]}"); ;
            }
        }

    }

}

单个工作,但是当我尝试填充结构数组时,它会失败。有人可以告诉我哪里出错了吗?

0 个答案:

没有答案