枚舉匹配VID PID的 HID裝置,並返回裝置路徑

來源:互聯網
上載者:User

 

 

#include <dbt.h>#include <wtypes.h>#include <sal_supp.h>#include <kernelspecs.h>#include <setupapi.h>extern "C"{#include <hidsdi.h>}#pragma comment(lib,"hid.lib")#pragma comment(lib,"setupapi.lib")BOOL EnumHIDDevice(WORD uVID, WORD uPID, //USB VID PID                   BOOL bPresentFlag, //裝置必須存在標誌 0不需要插入裝置                   TCHAR szDevPath[MAX_PATH+1] = NULL, //裝置路徑                   int iIndex=0) //第N個裝置 (對多個相同的裝置進行區分){  BOOL bRet = FALSE;  int iFoundCount = 0; //尋找到匹配裝置個數  GUID HidGuid;  HidD_GetHidGuid(&HidGuid);  DWORD dwFlage = DIGCF_DEVICEINTERFACE;  if(bPresentFlag)     dwFlage |= DIGCF_PRESENT;  HDEVINFO hdev = SetupDiGetClassDevs(&HidGuid,     NULL,     NULL,     dwFlage);  if (INVALID_HANDLE_VALUE != hdev)  {    int idev = 0;     while(! bRet)    {      SP_DEVICE_INTERFACE_DATA did = {0};      did.cbSize = sizeof(did);      if (SetupDiEnumDeviceInterfaces(hdev,        0,        &HidGuid,        idev,        &did))      {        DWORD cbRequired = 0;        SetupDiGetDeviceInterfaceDetail(hdev,          &did,          0,          0,          &cbRequired,          0);        if (ERROR_INSUFFICIENT_BUFFER == GetLastError())        {          PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd =            (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,  cbRequired);          if (pdidd)          {            pdidd->cbSize = sizeof(*pdidd);            if (SetupDiGetDeviceInterfaceDetail(hdev,              &did,              pdidd,              cbRequired,              &cbRequired,              0))            {              TRACE(_T("\n%s\n"), pdidd->DevicePath);              // Enumerated a battery.  Ask it for information.              HANDLE hDevHandle =                 CreateFile(pdidd->DevicePath,                GENERIC_READ | GENERIC_WRITE,                FILE_SHARE_READ | FILE_SHARE_WRITE,                NULL,                OPEN_EXISTING,                FILE_ATTRIBUTE_NORMAL,                NULL);              if (INVALID_HANDLE_VALUE != hDevHandle)              {                // Ask the battery for its tag.                HIDD_ATTRIBUTES hidAttributes = {0};                                hidAttributes.Size = sizeof(hidAttributes);                if (HidD_GetAttributes(hDevHandle, &hidAttributes))                {                  if( hidAttributes.VendorID == uVID                    && hidAttributes.ProductID == uPID)                  {                     WCHAR szManufacturer[MAX_PATH+1]={0};                    WCHAR szProduct[MAX_PATH+1]={0};                    WCHAR szSerialNumber[MAX_PATH+1]={0};                    HidD_GetManufacturerString(hDevHandle, szManufacturer, MAX_PATH);                    HidD_GetProductString(hDevHandle, szProduct, MAX_PATH);                    HidD_GetSerialNumberString(hDevHandle, szSerialNumber, MAX_PATH);                    TRACE(_T("Manufacturer string=%s\n"), (LPCTSTR)CString(szManufacturer));                    TRACE(_T("Product string=%s\n"), (LPCTSTR)CString(szProduct));                    TRACE(_T("SerialNumber=%s\n"), (LPCTSTR)CString(szSerialNumber));                    if(iFoundCount == iIndex)                    {                      if(szDevPath)                      {                        _tcscpy_s(szDevPath, MAX_PATH, pdidd->DevicePath);                      }                      bRet = TRUE;                    }                    iFoundCount++;                  }                }                CloseHandle(hDevHandle);              }              else              {                DWORD dwErr = GetLastError();                TRACE(_T("CreateFile failed with code %lu\n"), dwErr);              }            }            else            {              DWORD dwErr = GetLastError();              TRACE(_T("SetupDiGetDeviceInterfaceDetail failed with code %lu\n"), dwErr);            }            LocalFree(pdidd);          }        }      }      else  if (ERROR_NO_MORE_ITEMS == GetLastError())      {        break;  // Enumeration failed - perhaps there are no more items      }      idev++; //for next device    }    SetupDiDestroyDeviceInfoList(hdev);  }  return bRet;}

 

//測試代碼  TCHAR szDevPath[MAX_PATH+1] = {0};  if(EnumHIDDevice(0x046d, 0xC018, TRUE, szDevPath))  {    ::MessageBox(NULL, szDevPath, _T("Hid Device"), MB_OK);  }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.