C ++结构到C#结构 - 需要帮助 [英] C++ structure to C# structure - help needed

查看:79
本文介绍了C ++结构到C#结构 - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将一些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 use
them in the code, I get errors in "internal static BluetoothDeviceInfo
Create()". 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_ADDRESS
BYTE 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 the
sizeof(BLUETOOTH_DEVICE_INFO)
BLUETOOTH_ADDRESS Address; // Bluetooth address
ULONG ulClassofDevice; // Bluetooth "Class of Device"
BOOL fConnected; // Device connected/in use
BOOL fRemembered; // Device remembered
BOOL fAuthenticated; // Device authenticated/paired/bonded
SYSTEMTIME stLastSeen; // Last time the device was seen
SYSTEMTIME stLastUsed; // Last time the device was used for other than
RNR, inquiry, or SDP
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ]; // Name of the device
} BLUETOOTH_DEVICE_INFO_STRUCT;
#define BLUETOOTH_DEVICE_INFO BLUETOOTH_DEVICE_INFO_STRUCT
typedef 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 be
marshaled as an unmanaged structure; no meaningful size or offset can be
computed."
bdi.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceInfo));
bdi.szName = new char[32];
return bdi;
}
}
/*
typedef struct _BLUETOOTH_DEVICE_SEARCH_PARAMS {
DWORD dwSize; // IN sizeof this structure
BOOL fReturnAuthenticated; // IN return authenticated devices
BOOL fReturnRemembered; // IN return remembered devices
BOOL fReturnUnknown; // IN return unknown devices
BOOL fReturnConnected; // IN return connected devices
BOOL fIssueInquiry; // IN issue a new inquiry
UCHAR cTimeoutMultiplier; // IN timeout for the inquiry
HANDLE hRadio; // IN handle to radio to enumerate - NULL == all radios
will 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.

推荐答案

我想你将不得不添加一个属性告诉它明确的BluetoothAddress类的大小

这样当你调用sizeof对抗

时,它会知道如何添加内容(因为你引用了

内部的BluetoothAddress类)


因为你告诉它你将明确管理

蓝牙地址的布局,你在相同的偏移处开始不同的领域,它实际上没有办法知道它有多大。如果你将它标记为

constanct大小为8,那么它就会知道。

I think you''re going to have to add an attribute that tells it the size of
the BluetoothAddress class explicitly so that when you call sizeof against
the BluetoothDeviceInfo it knows how to add things up (since you referenced
the BluetoothAddress class internally)

Since you''re telling it that you''ll explicitly manage the layout of a
BluetoothAddress, and you''re starting different fields at the same offset, it
really has no way to know how big the thing is. If you tag it as being a
constanct size of 8, then it will know.


嗨Scott,

感谢您的回复。


现在,我得到了工作,但我收到了一个新错误:


类型''System.TypeLoadException''未处理的异常发生在

System.Net.Bluetooth.exe


附加信息:无法加载类型

来自程序集System.Net.Bluetooth的System.Net.Bluetooth.BluetoothAddress,

Version = 1.0.1787.23495,Culture = neutral,PublicKeyToken = null因为它

包含偏移0处的对象字段,该字段未正确对齐或

与非对象字段重叠。


任何想法?我猜这是蓝牙地址结构(联盟)的错误。

但是我不知道出了什么问题。


TIA。


B Vidyadhar Joshi


" Scott" <钪*** @ discussions.microsoft.com>在消息中写道

news:63 ********************************** @ microsof t.com ...
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new error:

An unhandled exception of type ''System.TypeLoadException'' occurred in
System.Net.Bluetooth.exe

Additional information: Could not load type
System.Net.Bluetooth.BluetoothAddress from assembly System.Net.Bluetooth,
Version=1.0.1787.23495, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct (union).
But can''t figure out what is wrong.

TIA.

B Vidyadhar Joshi

"Scott" <Sc***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
我认为你必须添加一个属性,告诉它明确的BluetoothAddress类的大小,这样当你调用sizeof对抗
因为你告诉它你会明确地管理它一个
BluetoothAddress的布局,你在相同的偏移处开始不同的领域,
它真的无法知道它有多大。如果你将它标记为
constanct大小为8,那么它就会知道。
I think you''re going to have to add an attribute that tells it the size of
the BluetoothAddress class explicitly so that when you call sizeof against
the BluetoothDeviceInfo it knows how to add things up (since you
referenced
the BluetoothAddress class internally)

Since you''re telling it that you''ll explicitly manage the layout of a
BluetoothAddress, and you''re starting different fields at the same offset,
it
really has no way to know how big the thing is. If you tag it as being a
constanct size of 8, then it will know.





" B Vidyadhar Joshi <乔***** @ knowledgepointsolutions.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
嗨斯科特,

感谢您的回复。

现在,我得到了工作,但我收到了一个新错误:

类型'未处理的异常' System.Net.Bluetooth.exe中发生'System.TypeLoadException''

附加信息:无法加载类型从组件System.Net加载System.Net.Bluetooth.BluetoothAddress .Bluetooth,
Version = 1.0.1787.23495,Culture = neutral,PublicKeyToken = null因为它包含偏移0处的对象字段,该字段未正确对齐或与非对象字段重叠。

任何想法?我猜这是BluetoothAddress结构的错误
(联合)。但是不能弄清楚出了什么问题。
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new error:

An unhandled exception of type ''System.TypeLoadException'' occurred in
System.Net.Bluetooth.exe

Additional information: Could not load type
System.Net.Bluetooth.BluetoothAddress from assembly System.Net.Bluetooth,
Version=1.0.1787.23495, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct
(union). But can''t figure out what is wrong.




这不行,你不能与非对象字段(值)重叠.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 an
object 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 as
a byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.

Willy.



这篇关于C ++结构到C#结构 - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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