【问题标题】:My SetupDiEnumDeviceInterfaces is not working我的 SetupDiEnumDeviceInterfaces 不工作
【发布时间】:2012-05-22 04:59:29
【问题描述】:

我很想知道我这样做是否正确:

//DeviceManager.h
#include <windows.h>
//#include <hidsdi.h>
#include <setupapi.h>
#include <iostream>
#include <cfgmgr32.h>
#include <tchar.h>
#include <devpkey.h>

extern "C"{
    #include <hidsdi.h>
}

//#pragma comment (lib, "setupapi.lib")

class DeviceManager
{
public:
    DeviceManager();
    ~DeviceManager();

    void ListAllDevices();
    void GetDevice(std::string vid, std::string pid);

    HANDLE PSMove;
    byte reportBuffer;
private:
    HDEVINFO deviceInfoSet;             //A list of all the devices
    SP_DEVINFO_DATA deviceInfoData;     //A device from deviceInfoSet

    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;

};

//DeviceManager.cpp
#include"DeviceManager.h"

DeviceManager::DeviceManager()
{
    deviceInfoSet = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); //Gets all Devices
}

DeviceManager::~DeviceManager()
{
}

void DeviceManager::ListAllDevices()
{
    DWORD deviceIndex = 0;

    deviceInfoData.cbSize = sizeof(deviceInfoData);

    while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
    {
        deviceInfoData.cbSize = sizeof(deviceInfoData);

        ULONG tcharSize;
        CM_Get_Device_ID_Size(&tcharSize, deviceInfoData.DevInst, 0);
        TCHAR* deviceIDBuffer = new TCHAR[tcharSize];   //the device ID will be stored in this array, so the tcharSize needs to be big enough to hold all the info.
                                                        //Or we can use MAX_DEVICE_ID_LEN, which is 200

        CM_Get_Device_ID(deviceInfoData.DevInst, deviceIDBuffer, MAX_PATH, 0); //gets the devices ID - a long string that looks like a file path.

        /*
        //SetupDiGetDevicePropertyKeys(deviceInfoSet, &deviceInfoData, &devicePropertyKey, NULL, 0, 0);
        if( deviceIDBuffer[8]=='8' && deviceIDBuffer[9]=='8' && deviceIDBuffer[10]=='8' && deviceIDBuffer[11]=='8' && //VID
            deviceIDBuffer[17]=='0' && deviceIDBuffer[18]=='3' && deviceIDBuffer[19]=='0' && deviceIDBuffer[20]=='8') //PID
        {
            std::cout << deviceIDBuffer << "\t<-- Playstation Move" << std::endl;
        }
        else
        {
            std::cout << deviceIDBuffer << std::endl;
        }*/

        std::cout << deviceIDBuffer << std::endl;

        deviceIndex++;
    }
}

void DeviceManager::GetDevice(std::string vid, std::string pid)
{

    DWORD deviceIndex = 0;
    deviceInfoData.cbSize = sizeof(deviceInfoData);

    while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
    {
        deviceInfoData.cbSize = sizeof(deviceInfoData);

        ULONG IDSize;
        CM_Get_Device_ID_Size(&IDSize, deviceInfoData.DevInst, 0);

        TCHAR* deviceID = new TCHAR[IDSize];

        CM_Get_Device_ID(deviceInfoData.DevInst, deviceID, MAX_PATH, 0);

        if( deviceID[8]==vid.at(0) && deviceID[9]==vid.at(1) && deviceID[10]==vid.at(2) && deviceID[11]==vid.at(3) && //VID
            deviceID[17]==pid.at(0) && deviceID[18]==pid.at(1) && deviceID[19]==pid.at(2) && deviceID[20]==pid.at(3)) //PID
        {
            //DWORD requiredBufferSize;
            //SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &requiredBufferSize, 


            HDEVINFO deviceInterfaceSet = SetupDiGetClassDevs(&deviceInfoData.ClassGuid, NULL, NULL, DIGCF_ALLCLASSES);

            DWORD deviceInterfaceIndex = 0;
            deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
            while(SetupDiEnumDeviceInterfaces(deviceInterfaceSet, NULL, &deviceInterfaceData.InterfaceClassGuid, deviceInterfaceIndex, &deviceInterfaceData))
            {
                deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
                std::cout << deviceInterfaceIndex << std::endl;

                deviceInterfaceIndex++;
            }

            //std::cout << deviceInterfaceData.cbSize << std::endl;

            break;
        }

        deviceIndex++;
    }
}

我的 SetupDiEnumDeviceInterfaces(在 GetDevice() 函数中)没有做任何事情。它甚至没有进入while循环。我做错了什么?

编辑

我刚刚调用了 GetLastError() 函数,它返回了 259 - ERROR_NO_MORE_ITEMS。设备甚至可能不包含接口吗?

【问题讨论】:

  • SetupDiEnumDeviceInfo 然后返回 false。查看文档以确定为什么会这样,然后使用您的调试器。如果你仍然无法弄清楚,那么就问一个更具体的问题,关于你当时卡在哪里。
  • 那么请问 deviceInfoData.ClassGuid 是否正确传递?我的意思是,我应该通过那个 guid 来获取 deviceInterfaceSet?还是我应该给它传递一个不同的 GUID?
  • 如果您从未进入 while 循环,那么您永远不会到达那行代码,这意味着您使用的是在构造函数中分配的 deviceInfoSet。提示:使用FormatMessage 和GetLastError 来确定调用SetupDiEnumDeviceInfo 后出现的具体问题可能只是系统上没有可用的活动USB 设备。
  • 已顺利编译并运行您的代码;例如:DeviceManager::ListAllDevices 为我返回:003796F8 00379800 ~我认为你没有设备 ;) ~最好在调用 SetupDiGetClassDevs时检查返回值@
  • @violet313 我创建了一个 DeviceManager 对象并调用了 ListAllDevices 函数,它显示了一个 USB 设备 ID 的列表:S...

标签: c++ device hid wdk


【解决方案1】:

试试这个。

我试图尽可能少地破解你的原始代码;以下代码(至少对我而言)进入内部while(SetupDiEnumDeviceInterfaces..):

void DeviceManager::GetDeviceUSB(std::string vid, std::string pid)
{
    DWORD deviceIndex = 0;
    deviceInfoData.cbSize = sizeof(deviceInfoData);

    //buried somewhere deep in the ddk
    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}};

    //get usb device interfaces
    HDEVINFO deviceInterfaceSet=SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_DEVICEINTERFACE);


    while(SetupDiEnumDeviceInfo(deviceInterfaceSet, deviceIndex, &deviceInfoData))
    {
        deviceInfoData.cbSize = sizeof(deviceInfoData);

        ULONG IDSize;
        CM_Get_Device_ID_Size(&IDSize, deviceInfoData.DevInst, 0);

        TCHAR* deviceID = new TCHAR[IDSize];

        CM_Get_Device_ID(deviceInfoData.DevInst, deviceID, MAX_PATH, 0);

        if( deviceID[8]==vid.at(0) && deviceID[9]==vid.at(1) && deviceID[10]==vid.at(2) && deviceID[11]==vid.at(3) && //VID
            deviceID[17]==pid.at(0) && deviceID[18]==pid.at(1) && deviceID[19]==pid.at(2) && deviceID[20]==pid.at(3)) //PID
        {
            DWORD deviceInterfaceIndex = 0;
            deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);

            while(SetupDiEnumDeviceInterfaces(deviceInterfaceSet, &deviceInfoData, &GUID_DEVINTERFACE_USB_DEVICE, deviceInterfaceIndex, &deviceInterfaceData))
            {
                deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
                std::cout << deviceInterfaceIndex << std::endl;

                //get some more details etc
                //DWORD requiredBufferSize;
                //SetupDiGetDeviceInterfaceDetail(deviceInterfaceSet, &deviceInterfaceData, NULL, 0, &requiredBufferSize, 

                deviceInterfaceIndex++;
            }
        }

        deviceIndex++;
    }
}

据我所知,此方法使用与您的 OP 构造函数调用相同的设备(注意:我包括了一些其他有用的接口 guid):

deviceInfoSet = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); //Gets all Devices

但我这样做是为了获得设备接口:

// /coughs/ you might want to put back the DIGCF_PRESENT flag I removed for testing
HDEVINFO deviceInterfaceSet=SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_DEVICEINTERFACE); 

我还将deviceInfoData 传递给SetupDiEnumDeviceInterfaces,因为根据文档:

指向指定设备的 SP_DEVINFO_DATA 结构的指针 DeviceInfoSet 中的信息元素。该参数是可选的,并且 可以为 NULL。如果指定了这个参数, SetupDiEnumDeviceInterfaces 将枚举限制为 指定设备支持的接口。如果这 参数为 NULL,重复调用 SetupDiEnumDeviceInterfaces 返回有关与所有关联的接口的信息 DeviceInfoSet 中的设备信息元素。这个指针是 通常由 SetupDiEnumDeviceInfo 返回。

编辑

根据要求提供附加说明。您的 USB 设备有一个相关的setup 类和 interface 类:

来自device setup classes documentation:

设备安装程序类定义了安装设备所涉及的类安装程序和类共同安装程序

来自device interface classes documentation:

设备接口类是将设备和驱动程序功能导出到其他系统组件(包括其他驱动程序)的一种方式,如 以及用户模式应用程序

所以:

deviceInfoSet = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL,DIGCF_PRESENT|DIGCF_ALLCLASSES);

这是检索所有 setup 类信息集并在“USB”上进行过滤

你可以这样做:

deviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES|DIGCF_DEVICEINTERFACE);`

这会检索支持任何类的设备接口的设备的类信息集。 (应用仿真器 ID s/a“USB”似乎没有影响)。

但至关重要的是: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.

(注意:SP_DEVINFO_DATA.ClassGuid始终是设备的 setup 类的 GUID)

据我所知,您在调用SetupDiEnumDeviceInterfaces() 时仍需要提供InterfaceClassGuid。老实说,如果调用者提供可选的 DeviceInfoData,我真的不明白为什么这是必要的,但既然它都是封闭源代码,我怎么知道? :)

这里是info regarding GUID_DEVINTERFACE_USB_DEVICE

免责声明:我不为 M$ 工作;一定要对以上信息持怀疑态度,当然要自己研究。

【讨论】:

  • 有效,这正是我所需要的!非常感谢:D。如果您不介意,我还可以请您解释一下您在顶部声明的 GUID 吗?为什么我们在使用 TEXT("USB") 时需要它们?有什么区别?或者您能否至少给我一个链接,我可以在其中查找有关您声明的 GUID 的更多信息?谢谢:D
  • 不,请参阅修改后的答案,顺便说一句,我*不是alot XD
  • 我希望我能多次投票。您必须为这个答案付出了很多努力。我还有一条信息要添加,已经折腾了一天多的时间来寻找它:如果您的设备是 WinUSB 设备,那么您传递给 SetupDiEnumDeviceInterfaces() 的 GUID 将是注册表/inf 中的“DeviceInterfaceGUIDs”值文件。我非常怀疑这在任何地方都有记录。
【解决方案2】:

问题从如何调用 SetupDiGetClassDevs 开始。

如果您要获取设备路径,请使用 SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE ,,,)

如果 SetupDiGetClassDevs 在 MS Help 所说的 ClassGuid 是 指向设备设置类或设备接口类的 GUID 的指针。

包含文件 devguid.h 包含一组 GUID_DEVCLASS 值。这些不与您需要的 GUID_DEVINTERFACE_* 值相同。

使用 #include ,其中包含 ksuuids.h,您可以在其中找到 GUID_DEVINTERFACE_* 值。

我的网站上有更详细的说明,其中包含一些有助于正确枚举 USB 设备的源代码。

见http://pixcl.com/SetupDiEnumInterfaces_Fail.htm

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 2021-08-04
    • 1970-01-01
    • 2018-09-25
    • 2011-06-27
    • 2016-08-27
    • 2018-12-23
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多