Search code examples
c#carraysstructmarshalling

returning structs array from c to c#


Im making a c# program to plot a cubic spline calculated in c, this is the interop part of the program

my c# code:

    [DllImport(@"..\..\..\Debug\Spline_Core.dll")]
    static extern IntPtr mult_points(Point[] points, int points_count, double factor);

    public Form1()
    {
        InitializeComponent();

        var points = new[]
        {
            new Point { x = 2, y = 3 },
            new Point { x = 5, y = 7 },
        };

        var sum = mult_points(points, 2, 2);
        var mult = (Point[])Marshal.PtrToStructure(sum, typeof(Point[]));
    }

and the c code:

typedef struct point
{
    double x, y;
} Point;

extern __declspec(dllexport) Point* __stdcall mult_points(Point* points, int points_count, double factor)
{
    int i;

    Point* mult_points = (Point*)malloc(points_count * sizeof(Point));

    for (i = 0; i < points_count; i++)
        mult_points[i] = create_point(points[i].x * factor, points[i].y * factor);

    return mult_points;
}

I can pass the array of points through the parameters from c# to c, but i can't get it back.


Solution

  • Point* is technically just a pointer to a single Point struct. The marshaller does not know if it is an array or how many elements are in such an array. So your cast to Point[] is wrong.

    For better understanding see this simple example - instead of this code:

    var sum = mult_points(points, 2, 2);
    var mult = (Point[])Marshal.PtrToStructure(sum, typeof(Point[]));
    

    ... you can use something like this:

    IntPtr sum = mult_points(points, 2, 2);
    int structSize = Marshal.SizeOf(typeof(Point));
    for (int i = 0; i < numberOfPoints; i++)
    {
        IntPtr curr = sum + (i * structSize);
        Point pt = (Point)Marshal.PtrToStructure(curr, typeof(Point));
        // Add pt to List<Point> for example
    }
    

    It's not an ideal solution, but I hope you see the point. A better (more elegant) solution can be found here: How do I marshall a pointer to a pointer of an array of structures?