根据VID PID获取USB设备路径

 

 

#include <wtypes.h>
#include <setupapi.h>

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

BOOL EnumUSBDevice(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; //查找到匹配设备个数

  //
  DWORD dwFlage = DIGCF_ALLCLASSES|DIGCF_DEVICEINTERFACE;
  if(bPresentFlag) 
    dwFlage |= DIGCF_PRESENT;
  HDEVINFO hdev = SetupDiGetClassDevs(NULL, NULL, NULL, dwFlage);

  if (INVALID_HANDLE_VALUE != hdev)
  {
    SP_DEVINFO_DATA DeviceInfoData = {0};
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    DWORD DeviceIndex = 0;    
    while (SetupDiEnumDeviceInfo(hdev, DeviceIndex, &DeviceInfoData)) 
    {
      DeviceIndex++;

      int idev = 0; 
      while(1)
      {
        SP_DEVICE_INTERFACE_DATA did = {0};
        did.cbSize = sizeof(did);

        if(SetupDiEnumDeviceInterfaces(hdev, NULL, &DeviceInfoData.ClassGuid, idev, &did))
        {
          idev++;
          
          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))
              {
                OutputDebugString(pdidd->DevicePath);
                OutputDebugString(_T("\r\n"));

                if(uPID && uVID)
                {
                  LPTSTR pszVid = _tcsstr(pdidd->DevicePath, _T("vid_"));
                  LPTSTR pszPid = _tcsstr(pdidd->DevicePath, _T("pid_"));
                  if(pszVid && pszPid)
                  {
                    TCHAR szVID[5]={0}, szPID[5]={0};
                    _tcsncpy_s(szVID, pszVid+4, 4);
                    _tcsncpy_s(szPID, pszPid+4, 4);

                    UINT pid = (UINT)_tcstol(szPID, NULL, 16);
                    UINT vid = (UINT)_tcstol(szVID, NULL, 16);
                    if(pid == uPID && vid == uVID)
                    {
                      if(iFoundCount == iIndex)
                      {
                        if(szDevPath)
                        {
                          _tcscpy_s(szDevPath, MAX_PATH, pdidd->DevicePath);
                        }
                        bRet = TRUE;
                      }
                      iFoundCount++;
                    }
                  }
                }
              }
              LocalFree(pdidd);
            }
          }
        }
        else 
        {
          if (ERROR_NO_MORE_ITEMS == GetLastError())
          {
            break;  // Enumeration failed - perhaps there are no more items
          }
        }
      }
    }

    SetupDiDestroyDeviceInfoList(hdev);
  }
 
  return bRet;
}

int _tmain(int argc, _TCHAR* argv[])
{
  TCHAR szDevPath[MAX_PATH+1] = {0};
  if(EnumUSBDevice(0x403, 0x6001, TRUE, szDevPath))
  {
    MessageBox(NULL, szDevPath, _T("Usb Device"), MB_OK);
  }
	return 0;
}


 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值