1. 程式人生 > >根據VID PID獲取USB裝置路徑

根據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;
}