+ -

HID 枚举系统中所有HID设备信息代码示例

枚举系统中所有HID设备信息代码示例

#include<windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include<hidsdi.h>
#include<tchar.h>

#pragma comment(lib,"Setupapi.lib ")
#pragma comment(lib,"hid.lib ")


BOOL USBEnumDevice(OUT PTCHAR pDeviceName, IN LPGUID pGuid,IN int instance)
{

    HDEVINFO info = SetupDiGetClassDevs((GUID*)pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
    if (info == INVALID_HANDLE_VALUE)
    {
        printf("No HDEVINFO available for this GUID\n");
        return FALSE;
    }

    // Get interface data for the requested instance
    SP_INTERFACE_DEVICE_DATA ifdata;
    ifdata.cbSize = sizeof(ifdata);
    if (!SetupDiEnumDeviceInterfaces(info, NULL, (GUID*)pGuid, instance, &ifdata))
    {
        _tprintf(TEXT("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n"));
        SetupDiDestroyDeviceInfoList(info);
        return FALSE;
    }

    // Get size of symbolic link name
    DWORD ReqLen;
    SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
    PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new TCHAR[ReqLen]);
    if (ifDetail == NULL)
    {
        SetupDiDestroyDeviceInfoList(info);
        return FALSE;
    }

    // Get symbolic link name
    ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
    {
        SetupDiDestroyDeviceInfoList(info);
        delete ifDetail;
        return FALSE;
    }

    //printf("Symbolic link is %s\n", ifDetail->DevicePath);
    memcpy(pDeviceName, ifDetail->DevicePath, _tcslen(ifDetail->DevicePath) * sizeof(TCHAR));

    pDeviceName[_tcslen(ifDetail->DevicePath)] = TEXT('\0');

    delete [] ifDetail;
    SetupDiDestroyDeviceInfoList(info);

    return TRUE;
}


int main()
{

    GUID hidGuid;
    ::HidD_GetHidGuid((LPGUID)&hidGuid);                      // 取HID设备GUID
    for (int i = 0;; i++)
    {
        TCHAR strDevicePath[512] = { 0 };
        if (!USBEnumDevice(strDevicePath, &hidGuid, i))
        {
            break;
        }

        HANDLE tmp_DeviceHandle = CreateFile(strDevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
        if (tmp_DeviceHandle == INVALID_HANDLE_VALUE)
        {
            CloseHandle(tmp_DeviceHandle);
            continue;
        }

        //=============== Get Attribute ===============
        HIDD_ATTRIBUTES Attributes;
        ZeroMemory(&Attributes, sizeof(Attributes));
        Attributes.Size = sizeof(HIDD_ATTRIBUTES);
        if (!HidD_GetAttributes(tmp_DeviceHandle, &Attributes))
        {
            CloseHandle(tmp_DeviceHandle);
            continue;
        }

        _tprintf(TEXT("%d %s\n"), i, strDevicePath);
        _tprintf(TEXT("Vendor ID: %04x, Product ID:%04x\n"), Attributes.VendorID, Attributes.ProductID);

        /////////////////////////////////
        ULONG bufferLength = 126;
        TCHAR ProductString[126] = { 0 };;
        if (!HidD_GetProductString(tmp_DeviceHandle, ProductString, bufferLength))
        {
            CloseHandle(tmp_DeviceHandle);
            continue;
        }



        //=============== Get Capabilities ===============
        PHIDP_PREPARSED_DATA PreparsedData;
        if (!HidD_GetPreparsedData(tmp_DeviceHandle, &PreparsedData))
        {

            CloseHandle(tmp_DeviceHandle);
            printf("Cannot get the Preparsed Data...\n");
            continue;
        }

        HIDP_CAPS ReturnCapabilities;
        if (!HidP_GetCaps(PreparsedData, &ReturnCapabilities))
        {

            CloseHandle(tmp_DeviceHandle);
            printf("Cannot get the Cap Data...\n");
            continue;
        }



        // Normal USB Device
        if ((ReturnCapabilities.OutputReportByteLength == 0x40) &&
            (ReturnCapabilities.InputReportByteLength == 0x40) &&
            (ReturnCapabilities.Usage == 0x0001) &&
            (ReturnCapabilities.UsagePage == 0xFF00))
        {


        }

    }
}
微信公众号
字节流公众号  字节流QQ群:952873936
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by bytekits.com,汇天下文字,成非凡梦想!!!