Marshal struct to unmanaged array

Posted by Pedro on Stack Overflow See other posts from Stack Overflow or by Pedro
Published on 2010-03-31T10:29:09Z Indexed on 2010/03/31 10:33 UTC
Read the original article Hit count: 438

Filed under:
|
|
|

Hi guys,

I have a C# struct 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 with P/Invoke. Some methods expect a double[3] parameter.

The unmanaged C signature is something like

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

Is there any way to set up a P/Invoke signature so I can pass instances of my Vector struct and marshal them transparently to unmanaged double[3]? I would also need bidirectional marshaling as the unmanaged function needs to write the output to the argument array, so I guess I would need to marshal as LpArray.

Any ideas?

Thanks

Pedro

© Stack Overflow or respective owner

Related posts about pinvoke

Related posts about marshalling