C++中的union结构体怎么转换成C#?

C++中的union结构体怎么转换成C#? 


陪伴而非守候
浏览 773回答 2
2回答

白猪掌柜的

123456789101112 [StructLayout(LayoutKind.Explicit, Size=2)]struct TokenValue{[FieldOffset(0)][MarshalAs(UnmanagedType.U1)]public byte _cval;[FieldOffset(1)][MarshalAs(UnmanagedType.U1)] public byte _ival;[FieldOffset(0)]public ushort flags;}

慕桂英4014372

        public struct dash_generic_param         {             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]             public short[] raw_data;         }         //public struct dash_generic_param         //{         //    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]         //    public ushort[] values;         //    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]         //    public ushort[] reserved;         //    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]         //    public ushort[] limit;         //}         // 结构体按你需求,2选1         public struct DashParamPacket {             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 34)]             public ushort[] raw_data;         }         //public struct DashParamPacket {         //     public ushort pre_id;         //     public byte token;         //     public byte r1;         //     public dash_generic_param data;         //     public byte major_id;         //     public byte minor_id;         //     public ushort r2;         //}         // 例如c++库里接口名为GetPacket,DashParamPacket是c#定义的与C++中内存对齐的结构体         [DllImport("xxx.dll")]         public static extern bool GetPacket(ref DashParamPacket parm);         static void Main(string[] args)         {             DashParamPacket param = new DashParamPacket(); // C#里的结构体             GetPacket(ref param); // 如果C++接口定义的结构体指针,可以直接用引用             // 如果需要转void*使用,可以用marhal库来回转             IntPtr p = In
打开App,查看更多内容
随时随地看视频慕课网APP