本文介绍了C ++结构到C#结构 - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已将一些C ++结构转换为C#结构。但是,当我在代码中使用 时,我会在内部静态BluetoothDeviceInfo Create()中收到错误。转换工会时我觉得自己做错了什么。 有人可以帮忙吗? / * typedef ULONGLONG BTH_ADDR; typedef struct _BLUETOOTH_ADDRESS { union { BTH_ADDR ullLong; //更容易比较BLUETOOTH_NULL_ADDRESS BYTE rgBytes [6]; //分解时更容易格式化 }; } BLUETOOTH_ADDRESS_STRUCT; #define BLUETOOTH_ADDRESS BLUETOOTH_ADDRESS_STRUCT #定义BLUETOOTH_NULL_ADDRESS((ULONGLONG)0x0) * / [StructLayout(LayoutKind.Explicit)] 内部类BluetoothAddress { [MarshalAs(UnmanagedType.U8)] [FieldOffset(0)]内部ulong ullLong; [MarshalAs(UnmanagedType) .ByValArray,SizeConst = 6)] [FieldOffset(0)]内部字节[] rgBytes; 内部BluetoothAddress() { ullLong = 0; rgBytes = new byte [6]; } } / * typedef struct _BLUETOOTH_DEVICE_INFO { DWORD dwSize; //此结构的大小(以字节为单位) - 必须是 sizeof(BLUETOOTH_DEVICE_INFO) BLUETOOTH_ADDRESS地址; //蓝牙地址 ULONG ulClassofDevice; //蓝牙设备类 BOOL fConnected; //设备已连接/正在使用 BOOL fRemembered; //设备记住 BOOL fAuthenticated; //设备认证/配对/保税 SYSTEMTIME stLastSeen; //上次看到设备 SYSTEMTIME stLastUsed; //上次使用设备时,除了 RNR,查询或SDP WCHAR szName [BLUETOOTH_MAX_NAME_SIZE]; //设备名称 } BLUETOOTH_DEVICE_INFO_STRUCT; #define BLUETOOTH_DEVICE_INFO BLUETOOTH_DEVICE_INFO_STRUCT typedef BLUETOOTH_DEVICE_INFO * PBLUETOOTH_DEVICE_INFO; * / 内部结构SystemTime { 公共简称wYear; 公开简称wMonth; 公开简短wDayOfWeek; 公众简短wDay; 公开简短wHour; 公开简短wMinute; public short wSecond; public short wMilliseconds; } [StructLayout(LayoutKind.Sequential)] 内部结构BluetoothDeviceInfo { [MarshalAs(UnmanagedType.U4)] internal int dwSize; [MarshalAs(UnmanagedType.Struct)] 内部BluetoothAddress地址; 内部ulong ulClassOfDevice; 内部bool fConnected; 内部bool fRemembered; 内部bool fAuthenticated; inter nal SystemTime stLastSeen; 内部SystemTime stLastUsed; [MarshalAs(UnmanagedType.ByValArray,SizeConst = 32)] internal char [] szName; 内部静态BluetoothDeviceInfo创建() { BluetoothDeviceInfo bdi = new BluetoothDeviceInfo(); //在下一行引发错误 //类型System.Net.Bluetooth.BluetoothDeviceInfo不能 作为非托管结构封送;没有意义的大小或偏移量可以是 计算。 bdi.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceInfo)); bdi .szName = new char [32]; 返回bdi; } } / * typedef struct _BLUETOOTH_DEVICE_SEARCH_PARAMS { DWORD dwSize; //这个结构的大小 BOOL fReturnAuthenticated; //返回经过身份验证的设备 BOOL fReturnRemembered; //作为回报记忆的设备 BOOL fReturnUnknown; //返回未知设备 BOOL fReturnConnected; //返回连接设备 BOOL fIssueInquiry; // IN发出新的询问 UCHAR cTimeoutMultiplier; // IN超时查询 HANDLE hRadio; // IN处理无线电以进行枚举 - NULL ==所有无线电 将被搜索 } BLUETOOTH_DEVICE_SEARCH_PARAMS; * / [StructLayout(LayoutKind.Sequential)] 内部结构BluetoothDeviceSearchParams { [MarshalAs(UnmanagedType.U4)] 内部int dwSize; 内部bool fReturnAuthenticated; 内部bool fReturnRemembered; 内部bool fReturnUnknown; 内部bool fReturnConnected; 内部bool fIssueInquiry; 内部ushort cTimeoutMultiplier; 内部IntPtr hRadio; 内部静态BluetoothDeviceSearchParams创建(IntPtr hRadio) { BluetoothDeviceSearchParams dsp = new BluetoothDeviceSearchParams(); dsp.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceSearchParams)); dsp.hRadio = hRadio; 返回dsp; } } 谢谢&此致, B Vidyadhar Joshi。I have converted a few C++ structures to C# structures. However, when I usethem in the code, I get errors in "internal static BluetoothDeviceInfoCreate()". I feel I''m doing something wrong while converting the union.Could someone help please?/*typedef ULONGLONG BTH_ADDR;typedef struct _BLUETOOTH_ADDRESS {union {BTH_ADDR ullLong; // easier to compare again BLUETOOTH_NULL_ADDRESSBYTE rgBytes[ 6 ]; // easier to format when broken out};} BLUETOOTH_ADDRESS_STRUCT;#define BLUETOOTH_ADDRESS BLUETOOTH_ADDRESS_STRUCT#define BLUETOOTH_NULL_ADDRESS ( (ULONGLONG) 0x0 )*/[StructLayout(LayoutKind.Explicit)]internal class BluetoothAddress{[MarshalAs(UnmanagedType.U8)][FieldOffset(0)] internal ulong ullLong;[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)][FieldOffset(0)] internal byte[] rgBytes;internal BluetoothAddress(){ullLong = 0;rgBytes = new byte[6];}}/*typedef struct _BLUETOOTH_DEVICE_INFO {DWORD dwSize; // size, in bytes, of this structure - must be thesizeof(BLUETOOTH_DEVICE_INFO)BLUETOOTH_ADDRESS Address; // Bluetooth addressULONG ulClassofDevice; // Bluetooth "Class of Device"BOOL fConnected; // Device connected/in useBOOL fRemembered; // Device rememberedBOOL fAuthenticated; // Device authenticated/paired/bondedSYSTEMTIME stLastSeen; // Last time the device was seenSYSTEMTIME stLastUsed; // Last time the device was used for other thanRNR, inquiry, or SDPWCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ]; // Name of the device} BLUETOOTH_DEVICE_INFO_STRUCT;#define BLUETOOTH_DEVICE_INFO BLUETOOTH_DEVICE_INFO_STRUCTtypedef BLUETOOTH_DEVICE_INFO * PBLUETOOTH_DEVICE_INFO;*/internal struct SystemTime{public short wYear;public short wMonth;public short wDayOfWeek;public short wDay;public short wHour;public short wMinute;public short wSecond;public short wMilliseconds;}[StructLayout(LayoutKind.Sequential)]internal struct BluetoothDeviceInfo{[MarshalAs(UnmanagedType.U4)]internal int dwSize;[MarshalAs(UnmanagedType.Struct)]internal BluetoothAddress Address;internal ulong ulClassOfDevice;internal bool fConnected;internal bool fRemembered;internal bool fAuthenticated;internal SystemTime stLastSeen;internal SystemTime stLastUsed;[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]internal char[] szName;internal static BluetoothDeviceInfo Create(){BluetoothDeviceInfo bdi = new BluetoothDeviceInfo();//Throws an error on the next line//"Type System.Net.Bluetooth.BluetoothDeviceInfo can not bemarshaled as an unmanaged structure; no meaningful size or offset can becomputed."bdi.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceInfo));bdi.szName = new char[32];return bdi;}}/*typedef struct _BLUETOOTH_DEVICE_SEARCH_PARAMS {DWORD dwSize; // IN sizeof this structureBOOL fReturnAuthenticated; // IN return authenticated devicesBOOL fReturnRemembered; // IN return remembered devicesBOOL fReturnUnknown; // IN return unknown devicesBOOL fReturnConnected; // IN return connected devicesBOOL fIssueInquiry; // IN issue a new inquiryUCHAR cTimeoutMultiplier; // IN timeout for the inquiryHANDLE hRadio; // IN handle to radio to enumerate - NULL == all radioswill be searched} BLUETOOTH_DEVICE_SEARCH_PARAMS;*/[StructLayout(LayoutKind.Sequential)]internal struct BluetoothDeviceSearchParams{[MarshalAs(UnmanagedType.U4)]internal int dwSize;internal bool fReturnAuthenticated;internal bool fReturnRemembered;internal bool fReturnUnknown;internal bool fReturnConnected;internal bool fIssueInquiry;internal ushort cTimeoutMultiplier;internal IntPtr hRadio;internal static BluetoothDeviceSearchParams Create(IntPtr hRadio){BluetoothDeviceSearchParams dsp = new BluetoothDeviceSearchParams();dsp.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceSearchParams) );dsp.hRadio = hRadio;return dsp;}}Thanks & Regards,B Vidyadhar Joshi.推荐答案 这不行,你不能与非对象字段(值)重叠.NET中的一个 对象字段,它们的分配方式不同。 人们常犯的一个错误就是认为.NET结构与C相同 (或C ++)结构,它们不是。 要解决您的问题,您不需要结构,只需将您的地址声明为 a byte [8]并使用BitConverter.ToUInt64将其转换为ulong。 Willy。This won''t work, you can''t overlap a non-object field (a value) with anobject field in .NET, they are allocated differently.One common mistake people make is to think .NET structs are the same as C(or C++) structs, they are NOT .To solve your problem you don''t need a struct, just declare your address asa byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.Willy. 这篇关于C ++结构到C#结构 - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 23:30