我已经阅读了大约 100+ 篇关于此的帖子超过 2 周,让自己完全困惑。这是我在BlueSoleil堆栈上编写的包装器。
在 C++ 中
typedef char BTINT8;
typedef unsigned char BTUINT8;
typedef unsigned char BTUCHAR; /* extended ASII character, 0 - 255 */
typedef unsigned char BTBOOL;
typedef short BTINT16;
typedef unsigned short BTUINT16;
typedef long BTINT32;
typedef unsigned long BTUINT32;
typedef void * BTLPVOID;
typedef BTUINT32 BTDEVHDL;
typedef BTUINT32 BTSVCHDL;
typedef BTUINT32 BTCONNHDL;
typedef BTUINT32 BTSHCHDL;
typedef BTUINT32 BTSDKHANDLE;
typedef struct _BlueToothDevice
{
BTDEVHDL rmt_device_handle;
BTINT32 rmt_device_num;
BTUINT32 rmt_device_class;
BTUINT8 rmt_device_name[64];
} BLUETOOTHDEVICE;
typedef struct _BlueToothDevices
{
BTUINT32 num_rmt_devices;
BLUETOOTHDEVICE rmt_btd[100];
} BLUETOOTHDEVICES;
public ref class RemoteDeviceDiscovery
{
public:
int GetBTD(BLUETOOTHDEVICE btd);
};
在 C# 中
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BluetoothDevice
{
[MarshalAs(UnmanagedType.SysUInt)]
public UInt32 rmt_device_handle;
[MarshalAs(UnmanagedType.SysInt)]
public Int32 rmt_device_num;
[MarshalAs(UnmanagedType.SysUInt)]
public UInt32 rmt_device_class;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248)]
public IntPtr rmt_device_name;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BluetoothDevices
{
[MarshalAs(UnmanagedType.SysUInt)]
public UInt32 num_rmt_devices;
[MarshalAs(UnmanagedType.ByValArray)]
public List<BluetoothDevice> BluetoothDeviceInfo;
}
static void Main()
{
var rd = new RemoteDeviceDiscovery();
BluetoothDevice bluetoothDevice = new BluetoothDevice();
var i = rd.GetBTD(ref bluetoothDevice);
}
错误 3 参数 1:无法从"控制台包装测试.程序.蓝牙设备"转换为"BlueSoleilWrapper._BlueToothDevice"
所以有人可以指出我哪里出错了。
吉姆
更新
使用以下标头代码编译 c++/cli 包装器
public ref class RemoteDeviceDiscovery
{
/* Remote Device Discovery */
public:
int^ GetBTD(_BlueToothDevices % btds);
};
和 CPP 代码
array<Byte>^ MakeManagedArray(unsigned char* input, int len)
{
array<Byte>^ result = gcnew array<Byte>(len);
for (int i = 0; i < len; i++)
{
result[i] = input[i];
}
return result;
}
int^ RemoteDeviceDiscovery::GetBTD(_BlueToothDevices % btds)
{
unsigned char name[] = "Test";
_BlueToothDevice entry;
btds.num_rmt_devices = 1;
entry.rmt_device_class = 1;
entry.rmt_device_handle = 2;
entry.rmt_device_num = 3;
entry.rmt_device_name = MakeManagedArray(name, sizeof(name));
btds.rmt_btd[0] = gcnew _BlueToothDevice();
btds.rmt_btd[0] = %entry;
return 99;
}
问题现在出在 C# 代码中
var rd = new RemoteDeviceDiscovery();
_BlueToothDevices bluetoothDevices = new _BlueToothDevices();
var i = rd.GetBTD(bluetoothDevices);
var rmt_device_handle = bluetoothDevices.rmt_btd[0].rmt_device_handle;
编译时,我得到"GetBTD"不受该语言的支持,到目前为止我所看到的所有解决方案都不起作用。 感谢任何进一步的帮助。
吉姆
似乎您使用的是C++\CLI,而不是普通C++。如果你直接使用RemoteDeviceDiscovery
那么你必须实例化BLUETOOTHDEVICE
,而不是试图创建自己的结构。
如果您不能使用 BLUETOOTHDEVICE
,因为它不公开可见,您可以为RemoteDeviceDiscovery
类编写某种适配器,该适配器将获取您的结构并将其映射到BLUETOOTHDEVICE
上。
好的,得到了一个有效的答案! 非常感谢 rpeshkov 你为我指明了正确的方向,我很高兴我从中学到了一些东西。
在页眉中
public ref class RemoteDeviceDiscovery
{
/* Remote Device Discovery */
public:
int GetBTD(_BlueToothDevices ^% btds);
};
在 cpp 代码中
int RemoteDeviceDiscovery::GetBTD(_BlueToothDevices ^% btds)
{
unsigned char name0[] = "Test0";
unsigned char name1[] = "Test1";
_BlueToothDevice^ entry = gcnew _BlueToothDevice();
entry->rmt_device_class = 1;
entry->rmt_device_handle = 2;
entry->rmt_device_num = 3;
entry->rmt_device_name = MakeManagedArray(name0, sizeof(name0));
btds->num_rmt_devices = 2;
btds->rmt_btd[0] = gcnew _BlueToothDevice();
btds->rmt_btd[0] = entry;
entry = gcnew _BlueToothDevice();
entry->rmt_device_class = 4;
entry->rmt_device_handle = 5;
entry->rmt_device_num = 6;
entry->rmt_device_name = MakeManagedArray(name1, sizeof(name1));
btds->rmt_btd[1] = gcnew _BlueToothDevice();
btds->rmt_btd[1] = entry;
return 99;
}
最后在 C# 代码中(这当然只是开发框架的基本测试)
var rd = new RemoteDeviceDiscovery();
_BlueToothDevices bluetoothDevices = new _BlueToothDevices();
var i = rd.GetBTD(ref bluetoothDevices);
var rmt_device_handle = bluetoothDevices.rmt_btd[0].rmt_device_handle;
我建议查看定义类RemoteDeviceDiscovery
的程序集(Visual Studio 中的 F12 Microsoft),看看是否可以找到该程序集对BlueSoleilWrapper._BlueToothDevice
的定义。 然后直接使用该定义。 ConsoleWrapperTest.Program.BluetoothDevice
永远不会映射到BlueSoleilWrapper._BlueToothDevice
,无论您的定义与程序集中的定义有多接近。
替换.cpp代码
int RemoteDeviceDiscovery::GetBTD(_BlueToothDevices ^% btds)
{
unsigned char name0[] = "Test0";
unsigned char name1[] = "Test1";
_BlueToothDevice^ entry = gcnew _BlueToothDevice();
entry->rmt_device_class = 1;
entry->rmt_device_handle = 2;
entry->rmt_device_num = 3;
entry->rmt_device_name = MakeManagedArray(name0, sizeof(name0));
btds->num_rmt_devices = 2;
btds->rmt_btd[0] = gcnew _BlueToothDevice();
btds->rmt_btd[0] = entry;
entry = gcnew _BlueToothDevice();
entry->rmt_device_class = 4;
entry->rmt_device_handle = 5;
entry->rmt_device_num = 6;
entry->rmt_device_name = MakeManagedArray(name1, sizeof(name1));
btds->rmt_btd[1] = gcnew _BlueToothDevice();
btds->rmt_btd[1] = entry;
return 99;
}