MFC软件获取USB设备的制造商、产品、序列号

PVOID buf = malloc(100);
        char * buf1 = (char *)malloc(100);

        PVOID buf2 = malloc(100);
        char * buf3 = (char *)malloc(100);

        PVOID buf4 = malloc(100);
        char * buf5 = (char *)malloc(100);

        int num = 0;
        int num1 = 0;
        int num2 = 0;

        PHIDD_ATTRIBUTES a;

        UpdateData(false); //更新界面
        int Count = 0; //Total number of devices found
        DWORD strSize=0,requiredSize=0;
        BOOL result1,result2;

        //定义一些变量,以后会用到
        SP_DEVINFO_DATA DeviceInfoData;
        SP_DEVICE_INTERFACE_DATA  DeviceInterfaceData;
        PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
        
        
        //第一步:获取deviceID
        GUID deviceId;
        HidD_GetHidGuid(&deviceId);
        

        //第二步:获取设备信息
        HDEVINFO handle;
        handle = SetupDiGetClassDevs(&deviceId, NULL, NULL, (DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); //Get only HID devices

 //第三步:对所有的设备进行枚举

        result1=false; //定义一些变量
        result2=false;
        CString temp11="";
        while(1)
        {
  
                DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
                result1 = SetupDiEnumDeviceInterfaces(
                        handle,
                        NULL, // IN PSP_DEVINFO_DATA  DeviceInfoData,  OPTIONAL
                        &deviceId,
                        Count,
                        &DeviceInterfaceData
                         );   
                if(result1 == FALSE)
                {
                        break;
                }

       //获得设备详细数据(初步)
                result1 = SetupDiGetDeviceInterfaceDetail(handle,
                  &DeviceInterfaceData,
                  NULL,
                  0,
                  &strSize,
                  NULL);
                requiredSize=strSize;
                DeviceInterfaceDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
                DeviceInterfaceDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
                DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA);

 //再次获得详细数据
                SetupDiGetDeviceInterfaceDetail(handle,
                        &DeviceInterfaceData,
                        DeviceInterfaceDetailData,
                        strSize,
                        &requiredSize,
                        &DeviceInfoData);

                //获得设备路径(最重要的部分)
                //m_zf1=DeviceInterfaceDetailData->DevicePath;
                UpdateData(false);
                Count++;
        } 
 if(Count != 0)
        {
                //AfxMessageBox("进来了");
                a = (PHIDD_ATTRIBUTES)malloc(100);
                a->Size = sizeof(struct _HIDD_ATTRIBUTES);
                
                HANDLE hCom = CreateFile(DeviceInterfaceDetailData->DevicePath,
                        NULL,//GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED,
                        NULL);

                if (hCom == INVALID_HANDLE_VALUE)
                {             
                   AfxMessageBox("无效句柄!");
                }
                
                HidD_GetAttributes(hCom,a);
                //m_sz1 = a->VendorID;

 result2 = HidD_GetManufacturerString(hCom,buf,100);
                if(result2 == FALSE)
                {
                        AfxMessageBox("失败!");
                }
                result2 = HidD_GetProductString(hCom,buf2,100);
                if(result2 == FALSE)
                {
                        AfxMessageBox("失败!");
                }
                result2 = HidD_GetSerialNumberString(hCom,buf4,100);
                if(result2 == FALSE)
                {
                        AfxMessageBox("失败!");
                }
                
 while(1)
                {
                        if(buf1[num-1] != '\0')
                        {
                                if(num == 0)
                                {
                                        sprintf(buf1,"%s",(char *)buf);
                                }
                                else
                                {
                                        sprintf(buf1,"%s%s",buf1,(char *)buf + (2*num));
                                }
                                num++;
                        }
                        else
                        {
                                break;
                        }
                }

while(1)
                {
                        if(buf3[num1-1] != '\0')
                        {
                                if(num1 == 0)
                                {
                                        sprintf(buf3,"%s",(char *)buf2);
                                }
                                else
                                {
                                        sprintf(buf3,"%s%s",buf3,(char *)buf2 + (2*num1));
                                }
                                num1++;
                        }
                        else
                        {
                                break;
                        }
                }
 while(1)
                {
                        if(buf5[num2-1] == '\0')
                        {
                                if(num2 == 0)
                                {
                                        sprintf(buf5,"%s",(char *)buf4);
                                }
                                else
                                {
                                        sprintf(buf5,"%s%s",buf5,(char *)buf4 + (2*num1));
                                }
                                num2++;
                        }
                        else
                        {
                                if(num2 == 0)
                                {
                                        buf5 = "Not specified";
                                }
                                break;
                        }
                }
 m_zf1 = buf1;
                m_zf2 = buf3;
                m_zf3 = buf5;
        }
        UpdateData(false); 

关于HID USB键鼠类不能直接用RreadFile函数获取数据,可以用钩子函数钩取键盘和鼠标的状态来实现你得到你要的效果。

转载请附上链接,谢谢。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值