c++ - SetupDiEnumDeviceInterfaces 失败问题

标签 c++ windows handle

我在使用上述功能时遇到问题。我已经阅读了 MSDN 和文档,这是我想出的但是失败了。

因此,我使用 SetupDiGetClassDevs 制作了所有设备的列表,然后将其存储到一个句柄中。哪个工作正常然后下一步是使用 SetupDiEnumDeviceInterfaces 枚举每个设备并传递句柄值。这是我丢失它的地方,它总是返回 false。希望能就我出错的地方提供一些建议。谢谢阅读。

#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#pragma comment(lib,"SetupAPI") 
//
int main() 
{
    // Set up handles and data storage
    HDEVINFO hDevInfo;
    // Load GUID Classes
    static GUID GUID_DEVINTERFACE_USB_HUB={ 0xf18a0e88, 0xc30c, 0x11d0, {0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8} }; 
    static GUID GUID_DEVINTERFACE_USB_DEVICE ={ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } }; 
    static GUID GUID_DEVINTERFACE_USB_HOST_CONTROLLER={ 0x3abf6f2d, 0x71c4, 0x462a, {0x8a, 0x92, 0x1e, 0x68, 0x61, 0xe6, 0xaf, 0x27}}; 
    // 
    SP_DEVICE_INTERFACE_DATA ifdata;
    // Create a HDEVINFO with all present devices.
    hDevInfo = SetupDiGetClassDevs(NULL,0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);
    //
        if (INVALID_HANDLE_VALUE == hDevInfo)
        {
        return FALSE;
        }
        else
        {
        printf("Device info set handle for all devices attached to system: 0x%x\n", hDevInfo);
        }
    //
    BOOL bResult = TRUE;
    DWORD nCount = 0;
    //
        while (bResult)
        {
        //
            ifdata.cbSize=sizeof(ifdata);
            //  
            bResult = SetupDiEnumDeviceInterfaces(
            hDevInfo,  
            NULL,   
            &GUID_DEVINTERFACE_USB_DEVICE,   
            (ULONG)nCount,  
            &ifdata); 


        //
        if(!bResult)
            {       
            printf("Error Failed\n");       
            //fails here with error 6 invalid handle 
            }
        }



    // Cleanup
    SetupDiDestroyDeviceInfoList(hDevInfo);
    //
    system ("pause");
    //
    return 0;

}

最佳答案

您对 SetupDiGetClassDevs 的调用不正确,因为它返回的设备信息集元素最终缺少 SetupDiEnumDeviceInterfaces 需要工作的设备接口(interface)列表。

参见SetupDiGetClassDevs documentation on msdn的备注部分:

To return devices that support a device interface of any class, set the DIFCF_DEVICEINTERFACE flag, set the DIGCF_ALLCLASSES flag, and set ClassGuid to NULL. The function adds to the device information set a device information element that represents such a device and then adds to the device information element a device interface list that contains all the device interfaces that the device supports.

因此,通过添加 DIGCF_DEVICEINTERFACE 标志修复您的 SetupDiGetClassDevs 调用,例如:

hDevInfo = SetupDiGetClassDevs(NULL,0,0, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);

关于c++ - SetupDiEnumDeviceInterfaces 失败问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12485414/

相关文章:

c++ - 汽车贷款计算 (C++)

c++ - 为什么我的 C++ 程序中会出现死循环?

使用 Windows api 在控制台中居中文本?

delphi - 仅具有进程ID时关闭主应用程序窗口的问题

matlab - R2014a 或更高版本的 Simulink 中嵌入式 matlab 函数中的图形对象句柄数组

c++ - 在 Qt 安装程序框架中禁用 native 下一步按钮

c++ - 将派生类转换为基类时 dynamic_cast 失败

c++ - 如何以编程方式获取 Windows 进程的线程计数?

Windows Batch 认为凌晨 2 点是下午 5 点之后

java - 如何处理公共(public)抽象 boolean 响应