将结构从c ++函数返回到c#然后返回数组UPDATE

时间:2016-06-12 22:41:02

标签: c# c++-cli

我已经阅读了超过2周的100多篇帖子,让自己完全糊涂了。这是我在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:无法从'ConsoleWrapperTest.Program.BluetoothDevice'转换为'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'不受语言和我见过的所有解决方案都无法解决的问题所支持。感谢任何进一步的帮助。

吉姆

4 个答案:

答案 0 :(得分:1)

好像你正在使用C ++ \ CLI,而不是普通的C ++。如果您直接使用RemoteDeviceDiscovery,则必须实例化BLUETOOTHDEVICE,而不是尝试创建自己的结构。

如果您无法使用BLUETOOTHDEVICE,因为它不是公开可见的,您可以为RemoteDeviceDiscovery类编写某种适配器,它将采用您的结构并将其映射到BLUETOOTHDEVICE

答案 1 :(得分:1)

好的,得到了​​一个有效的答案!非常感谢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;

答案 2 :(得分:0)

我建议查看定义类RemoteDeviceDiscovery的程序集(Microsoft Visual Studio中的F12),看看是否可以找到该程序集的BlueSoleilWrapper._BlueToothDevice定义。然后直接使用该定义。 ConsoleWrapperTest.Program.BluetoothDevice永远不会映射到BlueSoleilWrapper._BlueToothDevice,无论您的定义与程序集中的定义有多接近。

答案 3 :(得分:0)

替换.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;
}