Marshal structure for an unmanaged array

I have a C # structure to represent a Cartesian vector, something like this:

public struct Vector  
{  
    private double x;  
    private double y;  
    private double z;  

    //Some properties/methods
}

Now I have an unmanaged C dll that I need to call using P / Invoke. Some methods expect a double parameter [3].

Unmanaged C Signature is similar to

void Cross(double a[3], double b[3], double c[3]);  

Is there a way to customize the P / Invoke signature so that I can pass instances of my Vector structure and marshal them transparently to an unmanageable double [3]? I will also need bidirectional marshaling, since an unmanaged function should write the output to an array of arguments, so I think I would need to marshal as LpArray.

+3
source share
2 answers

P/Invoke, :

[DllImport("blah.dll")]
private static extern void Cross(ref Vector a, ref Vector b, ref Vector c);
+4

, , -

[MarshalAs(...)]
[StructLayout(LayoutKind::Sequential, Pack=1)]
public struct Vector  
{  
    private double x;  
    private double y;  
    private double z;  

    //Some properties/methods
}

0

Source: https://habr.com/ru/post/1739285/


All Articles