查询蓝牙设备时SetupDiEnumDeviceInterfaces返回ERROR_INVALID_PARAMETER

时间:2015-10-05 20:55:13

标签: windows winapi bluetooth bluetooth-lowenergy

我正在尝试使用CreateFile()从Bluetooth Low Energy设备获取HANDLE。 因此,我需要提取设备的设备路径。 调用SetupDiEnumDeviceInterfaces时出现ERROR_INVALID_PARAMETER错误。似乎第二个参数(DeviceInfoData)有问题。 任何想法可能是什么问题?

已编辑:简化代码

HDEVINFO hDevInfo;
DWORD i;

// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT);

if (hDevInfo == INVALID_HANDLE_VALUE)
{
    // Insert error handling here.
    return;//1;
}
PSP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA;
DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);

for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
{
    DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);

    char detailDataBuf[0x100];
    PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)detailDataBuf;
    ULONG length;
    ULONG requiredLength = 0;
    bool bResult = FALSE;

    for(DWORD j = 0; j < 10; j++ )
    {
        SP_DEVICE_INTERFACE_DATA interfaceData;
        interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        bResult = SetupDiEnumDeviceInterfaces(hDevInfo, DeviceInfoData, &GUID_DEVCLASS_BLUETOOTH, j, &interfaceData );
        if (!bResult) {
            int lastError = GetLastError(); // always returns ERROR 259
            continue;
        }
        // Get the size of the buffer required to receive the device info
        SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &requiredLength, NULL );
        if( requiredLength >= sizeof( detailDataBuf ) )
            break;

        // Get the name of the device
        detailData->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA );
        length = requiredLength;
        bResult = SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, detailData, length, &requiredLength, NULL ) != 0;
        if( !bResult )
            break;
    }
}

EDITED2:DeviceInfoData传递NULL:这个简单的情况总是返回false

HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

bool bResult = FALSE;

for(DWORD j = 0; j < 10; j++ )
{
    SP_DEVICE_INTERFACE_DATA interfaceData;
    interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    bResult = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVCLASS_BLUETOOTH, j, &interfaceData );
    if (!bResult) {
        int lastError = GetLastError(); // ERROR 259
        continue;
    }
}

1 个答案:

答案 0 :(得分:0)

文档说:

  

DeviceInfoData [in,optional]

     

指向SP_DEVINFO_DATA结构的指针,该结构指定DeviceInfoSet中的设备信息元素。

换句话说,它必须指向deviceInfo的元素,而您传递的指针不会。如果您不想将结果过滤到设备信息集中特定设备的接口,请传递NULL

(请注意,这是一个输入参数,由“in”标签指示。输出通过第五个参数DeviceInterfaceData传递。)