Marshalling C# structure to C++ Using StructureToPtr

浪子不回头ぞ 提交于 2019-12-06 12:18:24

Try the following variant:

public struct DynamicState
{
    [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]
    public double[] Position;

    [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]
    public double[] Velocity;

    [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]
    public double[] Acceleration;

    [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]
    public double[] Attitude;

    [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]
    public double[] AngularVelocity;
}

Another option is to use fixed array available in unsafe code:

public unsafe struct DynamicState
{
    public fixed double Position[3];

    public fixed double Velocity[3];

    public fixed double Acceleration[3];

    public fixed double Attitude[3];

    public fixed double AngularVelocity[3];
}

P.S. A good guide on .Net interop can be found here: http://www.mono-project.com/Interop_with_Native_Libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!