【问题标题】:SetupDiEnumDeviceInterfaces returns ERROR_INVALID_PARAMETER when querying bluetooth devices查询蓝牙设备时,SetupDiEnumDeviceInterfaces 返回 ERROR_INVALID_PARAMETER
【发布时间】:2015-10-07 22:48:07
【问题描述】:

我正在尝试使用 CreateFile() 从蓝牙低功耗设备获取 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;
    }
}

【问题讨论】:

    标签: windows winapi bluetooth bluetooth-lowenergy


    【解决方案1】:

    文档说:

    DeviceInfoData [输入,可选]

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

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

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

    【讨论】:

    • 谢谢,我编辑了代码以指向 hDevInfo,但我仍然得到同样的错误。 SetupDiEnumDeviceInfo 不应该填充 DeviceInfoData 吗?
    • 新代码有同样的错误,即DeviceInfoData没有指向设备信息集的元素。关于您的评论,不,该功能不会填写DeviceInfoData。这是输入参数,不是输出参数。
    • 但它是SetupDiEnumDeviceInfo 的输出参数,用作SetupDiEnumDeviceInterfaces 的输入,对吧?我也尝试为 deviceInfo 传递 NULL,但这也不起作用。还有其他想法吗?
    • 我在原帖中添加了一个简单的例子,但失败了
    • “但它是...的输出参数”是的,在您第一次发布的代码中,问题是 DeviceInfoData 指向来自不同设备信息集的设备信息结构。在更新的代码中,它指向您自己分配的内存。两者都无效。现在您传入 NULL,您将收到错误 259(没有更多数据),这意味着代码正在运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多