编组一个包含指向另一个c ++结构的指针的c ++结构 [英] Marshalling of a c++ struct which contains pointers to another c++ struct

查看:78
本文介绍了编组一个包含指向另一个c ++结构的指针的c ++结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将C ++结构正确编组到C#中的问题.这些定义如下:

I have an issue concerning the correct marshalling of c++ structs into c#. These are defined as follows:

extern "C"
{
    //Geometry forms that could be used for envelope calculation
    enum GeometryForms  {RECTANGLE, TRIANGLE, ELLIPSE, CIRCLESECTOR};

    //Structure for coordinates 3D
    typedef struct Coordinates
    {
        double  longitude;      //longitude of the point in dezimal deg
        double  latitude;       //latitude of the point in dezimal deg
        double  altitude;       //heigth of the point in metres
    } Coordinates, *PCoordinates;


    //Envelope Data Struct
    typedef struct EnvelopeStruct
    {
        GeometryForms   GeometryAft;        // Geometry form of the aft section
        GeometryForms   GeometryBow;        // Geometry form of the bow section
        GeometryForms   GeometryCurve;      // Geometry form during direction change
        GeometryForms   Geometry3DAboveWL;  // Geometry form 3D under the water level
        GeometryForms   Geometry3DUnderWL;  // Geometry form 3D above the water level
        Coordinates     *PointsAft;         // Coordinates of the part of the envelope of the aft section
        Coordinates     *PointsBow;         // Coordinates of the part of the envelope of the bow section
        Coordinates     *PointsCurve;       // Coordinates of the envelope during direction change
        Coordinates     *Points3DAboveWL;   // Coordinates of the envelope above water level
        Coordinates     *Points3DUnderWL;   // Coordinates of the envelope under water level
    }EnvelopeStruct, *PEnvelopeStruct;


    //Structure includes all parameters that are used for the envelope calculation
    typedef struct CalculationParametersStruct
    {
        double  pos_long;           //Position of the vessel, longitude in decimal degree
        double  pos_lat;            //Position of the vessel, latitude in decimal degree
        double  pos_alt;            //Position of the vessel, altitude in metres
        double  pos_long_future;    //Predicted longitude of the vessel in decimal degree
        double  pos_lat_future;     //Predicted latitude of the vessel in decimal degree
        double  pos_alt_future;     //Predicted altitude of the vessel in metres
        float   pos_future_time;    //Time or timeinterval between actual position and predicted position
        double  sigma_long;         //Accuracy of actual longitude
        double  sigma_lat;          //Accuracy of actual latitude
        double  sigma_alt;          //Accuracy of actual altitude
        double  PL_long;            //Integrity value (protection level) into longitude
        double  PL_lat;             //Integrity value (protection level) into latitude
        double  length;             //length of the vessel in metres
        double  beam;               //beam of the vessel in metres
        double  draft;              //draft of the vessel in metres
        double  max_t;              //maximum trim
        double  v;                  //velocity of the vessel
        double  course;             //vessels heading
        double  rate;               //Rate of Turn of the vessel
        double  angle;              //Rudder Angle of the vessel
        double  coef;               //Maneuvering Coefficient
        double  scale;              //Dangerous Cargo Scale
        double  scale_lon;          
        double  scale_lat;
        double  wave_lon;
        double  wave_lat;
        double  ENC_reliability;            
    }CalculationParametersStruct, *PCalculationParametersStruct;

我试图用c#中的后续代码来完成,但是暂时不起作用:

I tried to accomplish with the subsequent code in c#, but it does not work for now:

public enum GeometryForms { RECTANGLE, TRIANGLE, ELLIPSE, CIRCLESECTOR };

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public class Coordinates
    {
        public double longitude;        //longitude of the point in decimal deg
        public double latitude;     //latitude of the point in decimal deg
        public double altitude;     //height of the point in metres
    }

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public class EnvelopeStruct
    {
        public GeometryForms GeometryAft;       // Geometry form of the aft section
        public GeometryForms GeometryBow;       // Geometry form of the bow section
        public GeometryForms GeometryCurve;     // Geometry form during direction change
        public GeometryForms Geometry3DAboveWL; // Geometry form 3D under the water level
        public GeometryForms Geometry3DUnderWL; // Geometry form 3D above the water level
        [MarshalAs(UnmanagedType.LPStruct, SizeConst = 96)]
        public Coordinates PointsAft;           // Coordinates of the part of the envelope of the aft section
        [MarshalAs(UnmanagedType.LPStruct, SizeConst = 96)]
        public Coordinates PointsBow;           // Coordinates of the part of the envelope of the bow section
        [MarshalAs(UnmanagedType.LPStruct, SizeConst = 4320)]
        public Coordinates PointsCurve;     // Coordinates of the envelope during direction change
        [MarshalAs(UnmanagedType.LPStruct, SizeConst = 96)]
        public Coordinates Points3DAboveWL; // Coordinates of the envelope above water level
        [MarshalAs(UnmanagedType.LPStruct, SizeConst = 96)]
        public Coordinates Points3DUnderWL; // Coordinates of the envelope under water level
    }

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public class CalculationParametersStruct
    {
        public double pos_long;         //Position of the vessel, longitude in decimal degree
        public double pos_lat;          //Position of the vessel, latitude in decimal degree
        public double pos_alt;          //Position of the vessel, altitude in metres
        public double pos_long_future;  //Predicted longitude of the vessel in decimal degree
        public double pos_lat_future;       //Predicted latitude of the vessel in decimal degree
        public double pos_alt_future;       //Predicted altitude of the vessel in metres
        public float pos_future_time;   //Time or timeinterval between actual position and predicted position
        public double sigma_long;           //Accuracy of actual longitude
        public double sigma_lat;            //Accuracy of actual latitude
        public double sigma_alt;            //Accuracy of actual altitude
        public double PL_long;          //Integrity value (protection level) into longitude
        public double PL_lat;               //Integrity value (protection level) into latitude
        public double length;               //length of the vessel in metres
        public double beam;             //beam of the vessel in metres
        public double draft;                //draft of the vessel in metres
        public double max_t;                //maximum trim
        public double v;                    //velocity of the vessel
        public double course;               //vessels heading
        public double rate;             //Rate of Turn of the vessel
        public double angle;                //Rudder Angle of the vessel
        public double coef;             //Maneuvering Coefficient
        public double scale;                //Dangerous Cargo Scale
        public double scale_lon;
        public double scale_lat;
        public double wave_lon;
        public double wave_lat;
        public double ENC_reliability;
    }

出现错误CS0021:尝试访问

I get an error CS0021: Cannot apply indexing with [] to an expression of type 'EnvelopeCalculatorWrapper.Coordinates' when I try to access

[MarshalAs(UnmanagedType.LPStruct, SizeConst = 96)]
            public Coordinates PointsAft;

作为c#中的数组,其方式为:

as an array in c# in the manner:

calc.envelope.PointsAft[i].longitude

有人知道怎么做!

提前谢谢!

干杯,斯蒂芬

推荐答案

您忘记将其声明为数组.添加一个构造函数以对其进行初始化:

You forgot to declare it as an array. Add a constructor to initialize them:

    [StructLayout(LayoutKind.Sequential)]
    public class EnvelopeStruct {
        public EnvelopeStruct() {
            this.PointsAft = new Coordinates[4];
            // etc..
        }
        public Coordinates[] PointsAft;
        // etc..
    }

不使用任何属性,默认封送处理保持原样.

Don't use any attributes, the default marshaling is good as-is.

这篇关于编组一个包含指向另一个c ++结构的指针的c ++结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆