C++中的union结构体怎么转换成C#?
2 回答
白猪掌柜的
TA贡献1893条经验 获得超10个赞
1 2 3 4 5 6 7 8 9 10 11 12 | [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
TA贡献1871条经验 获得超13个赞
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
- 2 回答
- 0 关注
- 772 浏览
添加回答
举报
0/150
提交
取消