2015-11-20 86 views
0

检索“设备实例路径”我跟了一些资源,包括this一个在那里我可以取回一串如PID,VID USB设备等方面的性能,但我需要检索“设备实例路径”,并且也因为它包含最终我想要的序列号(最后4位)从连接的USB设备

我试图使用here代码检索序列号,但它对USB存储设备是这样做的,我的是一个独立的设备,它有USB连接类似像机

QString USBDevices::findDevice() 
{ 
    GUID*pClassGuid = NULL; 
    LPCTSTR pszEnumerator = TEXT("USB"); 

    unsigned i, j; 
    DWORD dwSize, dwPropertyRegDataType; 
    DEVPROPTYPE ulPropertyType; 
    CONFIGRET status; 
    HDEVINFO hDevInfo; 
    SP_DEVINFO_DATA DeviceInfoData; 
    const static LPCTSTR arPrefix[3] = {TEXT("VID_"), TEXT("PID_"), TEXT("MI_")}; 
    TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN]; 
    TCHAR szDesc[1024], szHardwareIDs[4096]; 
    WCHAR szBuffer[4096]; 
    LPTSTR pszToken, pszNextToken; 
    TCHAR szVid[MAX_DEVICE_ID_LEN], szPid[MAX_DEVICE_ID_LEN], szMi[MAX_DEVICE_ID_LEN]; 
    FN_SetupDiGetDevicePropertyW fn_SetupDiGetDevicePropertyW = (FN_SetupDiGetDevicePropertyW) 
     GetProcAddress (GetModuleHandle (TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyW"); 

    // List all connected USB devices 
    hDevInfo = SetupDiGetClassDevs (pClassGuid, pszEnumerator, NULL, 
            pClassGuid != NULL ? DIGCF_PRESENT: DIGCF_ALLCLASSES | DIGCF_PRESENT); 


    //hDevInfo = SetupDiGetClassDevs(NULL,0,0, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE); 

    if (hDevInfo == INVALID_HANDLE_VALUE) 
     return ""; 

    // Find the ones that are driverless 
    for (i = 0; ; i++) { 
     DeviceInfoData.cbSize = sizeof (DeviceInfoData); 
     if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData)) 
      break; 

     status = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0); 
     if (status != CR_SUCCESS) 
      continue; 

     // Display device instance ID 
     qDebug() << QString::fromWCharArray(szDeviceInstanceID); 

     if (SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, 
               &dwPropertyRegDataType, (BYTE*)szDesc, 
               sizeof(szDesc), // The size, in bytes 
               &dwSize)) 
      qDebug() << " Device Description: " << QString::fromWCharArray(szDesc); 

     if (SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, 
               &dwPropertyRegDataType, (BYTE*)szHardwareIDs, 
               sizeof(szHardwareIDs), // The size, in bytes 
               &dwSize)) { 
      LPCTSTR pszId; 
      qDebug() << " Hardware IDs:\n"; 
      for (pszId=szHardwareIDs; 
       *pszId != TEXT('\0') && pszId + dwSize/sizeof(TCHAR) <= szHardwareIDs + ARRAYSIZE(szHardwareIDs); 
       pszId += lstrlen(pszId)+1) { 

       qDebug() << QString::fromWCharArray(pszId); 
      } 
     } 

     // Retreive the device description as reported by the device itself 
     // On Vista and earlier, we can use only SPDRP_DEVICEDESC 
     // On Windows 7, the information we want ("Bus reported device description") is 
     // accessed through DEVPKEY_Device_BusReportedDeviceDesc 
     if (fn_SetupDiGetDevicePropertyW && fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc, 
                      &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) { 

      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) 
      { 
       QString busReportedDeviceDescription = QString::fromWCharArray(szBuffer); 
       qDebug() << " Bus Reported Device Description: " << QString::fromWCharArray(szBuffer); 

       if (busReportedDeviceDescription == MY_DEVICE) 
       { 
        qDebug() << getSerialNum(hDevInfo); 
        return busReportedDeviceDescription; 
       } 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_Manufacturer, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) { 
       qDebug() << " Device Manufacturer: " << QString::fromWCharArray(szBuffer); 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_FriendlyName, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) { 
       qDebug() << " Device Friendly Name: " << QString::fromWCharArray(szBuffer); 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_LocationInfo, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) { 
       qDebug() << " Device Location Info: " << QString::fromWCharArray(szBuffer); 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_SecuritySDS, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) { 
       // See Security Descriptor Definition Language on MSDN 
       // (http://msdn.microsoft.com/en-us/library/windows/desktop/aa379567(v=vs.85).aspx) 
       qDebug() << " Device Security Descriptor String: " << QString::fromWCharArray(szBuffer); 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_ContainerId, 
               &ulPropertyType, (BYTE*)szDesc, sizeof(szDesc), &dwSize, 0)) { 
       StringFromGUID2((REFGUID)szDesc, szBuffer, ARRAY_SIZE(szBuffer)); 
       qDebug() << " ContainerId: " << QString::fromWCharArray(szBuffer); 
      } 
      if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_DeviceDisplay_Category, 
               &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) 
       qDebug() << " Device Display Category: " << QString::fromWCharArray(szBuffer); 
     } 

     QString deviceInstanceID(QString::fromWCharArray(szDeviceInstanceID)); 
     QRegExp splitBy("(\\\\|\\&|\\#)"); 
     QStringList deviceInstanceIDItems = deviceInstanceID.split(splitBy); 

     QString vid; 
     QString pid; 
     QString mi; 

     if (deviceInstanceIDItems.count() > 1) 
      vid = deviceInstanceIDItems[1].contains("VID_") ? deviceInstanceIDItems[1] : ""; 

     if (deviceInstanceIDItems.count() > 2) 
      pid = deviceInstanceIDItems[2].contains("PID_") ? deviceInstanceIDItems[2] : ""; 

     if (deviceInstanceIDItems.count() > 3) 
      mi = deviceInstanceIDItems[3].contains("MI_") ? deviceInstanceIDItems[3] : ""; 

     if (!vid.isEmpty()) 
      qDebug() << " vid: " << vid; 
     if (!pid.isEmpty()) 
      qDebug() << " pid: " << pid; 
     if (!mi.isEmpty()) 
      qDebug() << " mi: " << mi; 

    } 

    return ""; 
} 

QString USBDevices::getSerialNum(HDEVINFO hDevInfo) 
{ 
    // Get a context structure for the device interface 
     // of a device information set. 
     BYTE Buf[1024]; 
     PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = 
      (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf; 
     SP_DEVICE_INTERFACE_DATA   spdid; 
     SP_DEVINFO_DATA     spdd; 

     spdid.cbSize = sizeof(spdid); 

     DWORD dwIndex = 0; 
     while (true) 
     { 
      if (! SetupDiEnumDeviceInterfaces(hDevInfo, NULL, 
               //&GUID_DEVINTERFACE_USB_DISK, 
               &my_guid, 
               dwIndex, &spdid)) 
      { 
      qDebug() << "GetLastError code" << GetLastError(); 
      break; 
      } 

      DWORD dwSize = 0; 
      SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 
              0, &dwSize, NULL); 

      if ((dwSize != 0) && (dwSize <= sizeof(Buf))) 
      { 
      pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes! 

      ZeroMemory((PVOID)&spdd, sizeof(spdd)); 
      spdd.cbSize = sizeof(spdd); 

      long res = SetupDiGetDeviceInterfaceDetail(
       hDevInfo, &spdid, pspdidd, 
       dwSize, &dwSize, &spdd); 
//     if (res) 
//     { 
//     HANDLE hDrive = CreateFile(pspdidd->DevicePath,0, 
//            FILE_SHARE_READ | FILE_SHARE_WRITE, 
//            NULL, OPEN_EXISTING, 0, NULL); 
//     if (hDrive != INVALID_HANDLE_VALUE) 
//     { 
//      DWORD usbDeviceNumber = getDeviceNumber(hDrive); 

//      if (usbDeviceNumber == volumeDeviceNumber) 
//      { 
//       fprintf("%s", pspdidd->DevicePath); 
//      } 
//     } 
//     CloseHandle(hDrive); 
//     } 

      qDebug() << "Device Path" << pspdidd->DevicePath; 

      QString devicePath = QString::fromWCharArray(pspdidd->DevicePath); 

      return devicePath; 
      } 
      dwIndex++; 
     } 

     return ""; 
} 

问题是这个pa rt代码:

if (! SetupDiEnumDeviceInterfaces(hDevInfo, NULL, 
             //&GUID_DEVINTERFACE_USB_DISK, 
             &my_guid, 
             dwIndex, &spdid)) 
    { 
    qDebug() << "GetLastError code" << GetLastError(); 
    break; 
    } 

返回错误代码269,因此它不检索任何信息。我想提供几个我本以为这解决这个问题有不同的GUID,但它没有,我不知道我GUID应该提供反正因为我的不是存储设备。

回答

0

你应该叫SetupDiGetClassDevs启用标志DIGCF_DEVICEINTERFACE和一些接口类GUID。然后,您使用相同的GUID调用SetupDiEnumDeviceInterfaces。在你的情况下,我认为GUID_DEVINTERFACE_USB_DEVICE将完成这项工作。

符合条件的GUID,你可以找到here