WinAPI(C++)获取设备管理器中的设备

文章介绍了如何使用C++在Windows系统中通过SetupAPI和HIDDI库获取通过USB接口的HID设备和一般USB设备的硬件ID、VendorID和ProductID等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HID设备

获取windows设备管理器上所有通过usb接口接入的hid设备(设备管理器: 人体学输入设备)中显示的设备:

#include <windows.h>
#include <setupapi.h>
#include <hidsdi.h>
#include <iostream>
#include <locale>
#include <codecvt>

#pragma comment (lib, "setupapi.lib")
#pragma comment (lib, "hid.lib")
int get_all_hid_devices()
{
    GUID InterfaceClassGuid = { 0x4D1E55B2, 0xF16F, 0x11CF,{0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30} };
    HDEVINFO hDevInfo;
    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
    ULONG requiredSize = 0;

    hDevInfo = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (hDevInfo == INVALID_HANDLE_VALUE) 
        return 1;

    deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &InterfaceClassGuid, i, &deviceInterfaceData); ++i) {

        SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0, &requiredSize, NULL);
        deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
        deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, deviceInterfaceDetailData, requiredSize, &requiredSize, NULL)) {
            HANDLE hDevice = CreateFile(deviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
            if (hDevice != INVALID_HANDLE_VALUE) {
                HIDD_ATTRIBUTES attributes;
                HidD_GetAttributes(hDevice, &attributes);
                printf("VendorID: %04x ProductID: %04x Version: %02x \n", attributes.VendorID, attributes.ProductID, attributes.VersionNumber);
                CloseHandle(hDevice);
            }
        }
        free(deviceInterfaceDetailData);
    }

    SetupDiDestroyDeviceInfoList(hDevInfo);

    return 0;
}

int main()
{
    get_all_hid_devices();
    return 0;
}

打印:

VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 045e ProductID: 0000 Version: 00
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 045e ProductID: 0000 Version: 00
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 026d ProductID: 0002 Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 06cb ProductID: 000f Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 06cb ProductID: 000f Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 046d ProductID: c542 Version: 302
VendorID: 8087 ProductID: 0a1e Version: 200

USB设备

获取USB接入的USB设备(设备管理器: USB连接器管理器):

#include <windows.h>
#include <setupapi.h>
#include <hidsdi.h>
#include <iostream>
#include <locale>
#include <codecvt>

#pragma comment (lib, "setupapi.lib")
void PrintDeviceDetails(const TCHAR* hardwareID)
{
    // 不同的设备有不同的信息,不能通用处理,具体看设备信息取其中的字段
    if (hardwareID) {
        printf("\nHardware ID: %S", hardwareID);
    }
}

void EnumerateDevices()
{
    HDEVINFO deviceInfoSet = SetupDiGetClassDevs(
        (GUID*)&GUID_DEVCLASS_USB, NULL, NULL, DIGCF_PRESENT);

    if (deviceInfoSet == INVALID_HANDLE_VALUE)
        return;

    SP_DEVINFO_DATA deviceInfoData;
    deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, &deviceInfoData); i++)
    {
        DWORD dataSize = 0;
        SetupDiGetDeviceRegistryProperty(deviceInfoSet, &deviceInfoData,
            SPDRP_HARDWAREID, NULL, NULL, 0, &dataSize);
        TCHAR* hardwareID = (TCHAR*)alloca(dataSize);
        if (SetupDiGetDeviceRegistryProperty(deviceInfoSet, &deviceInfoData,
            SPDRP_HARDWAREID, NULL, (PBYTE)hardwareID, dataSize, &dataSize))
        {
            PrintDeviceDetails(hardwareID);
        }
    }

    SetupDiDestroyDeviceInfoList(deviceInfoSet);
}

输出打印:

Hardware ID: USB\ROOT_HUB30&VID8086&PID43ED&REV0011
Hardware ID: USB\VID_026D&PID_0002&REV_0101
Hardware ID: PCI\VEN_8086&DEV_9A17&SUBSYS_22D717AA&REV_05
Hardware ID: USB\ROOT_HUB30&VID8086&PID9A17&REV0005
Hardware ID: USB\VID_04F2&PID_B6BE&REV_6118
Hardware ID: hid\vid_1ff7&pid_0f11
Hardware ID: PCI\VEN_8086&DEV_43ED&SUBSYS_22D717AA&REV_11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值