2011-05-13 61 views
2

在Windows中,如果我打開設備管理器 - >右鍵單擊設備 - >屬性 - >詳細信息,我會得到{屬性,值}對。我想在Visual Studio中的C++代碼中訪問它們。我如何得到它?如何在C++中獲取windows中的設備屬性?

感謝,

+0

屬性是你尋找這對於具體的? – fardjad 2011-05-13 10:09:27

+0

@fardjad我需要知道:**匹配設備ID **。你可以請幫助 – 2015-09-02 12:28:45

回答

4

嘗試SetupDi_功能,尋找here例如。

HDEVINFO WinDeviceHelper::getDevInfoForClass(QString devClassName,DWORD& dwCount) 
{ 

    //DWORD dwGuids = 0; 

    SetupDiClassGuidsFromNameW(qPrintableW(devClassName), 0, 0, &dwCount); 

    //emit sSearchStarted(dwGuids); 

    if(dwCount) 
    { 
     GUID* pGuids = new GUID[dwCount]; 

     BOOL success = SetupDiClassGuidsFromNameW(qPrintableW(devClassName), pGuids, dwCount, &dwCount); 

     HDEVINFO hDevInfoSet = SetupDiGetClassDevsW(pGuids, NULL, NULL, DIGCF_PRESENT); 

     delete [] pGuids; 

     return hDevInfoSet; 
    } 
    else 
    { 
     return NULL; 
    } 
} 
bool WinDeviceHelper::getDeviceRegistryString(HDEVINFO hDevInfoSet,SP_DEVINFO_DATA &devInfo,DWORD propertyType,QString& propValue) 
{ 
    DWORD dwType = 0; 
    DWORD requiredSize=0; 
    propValue=""; 
    BOOL result=SetupDiGetDeviceRegistryPropertyW(hDevInfoSet, &devInfo, propertyType, &dwType, NULL, NULL, &requiredSize); 
    if ((result==ERROR_INVALID_DATA) || ((dwType!=REG_MULTI_SZ)&&(dwType!=REG_SZ)) || (requiredSize==0)) 
    { 
     return false; 
     //throw std::exception(__FILE__":"__LINE__" "__FUNCDNAME__": Error reading registry info"); 
    } 
    size_t strSize=requiredSize/sizeof(wchar_t)+1; 
    wchar_t* requestedData = new wchar_t[strSize];// буфер 
    result=SetupDiGetDeviceRegistryPropertyW(hDevInfoSet, &devInfo, propertyType, &dwType,reinterpret_cast<PBYTE>(requestedData), requiredSize, &requiredSize); 
    if(result==TRUE) 
    { 
     propValue=QString::fromWCharArray(requestedData,wcslen(requestedData)); 
    } 
    else 
    { 
     Logger::logError(QString("WinDeviceHelper::getDeviceRegistryString: SetupDiGetDeviceRegistryPropertyW failed with error %1").arg(GetLastError())); 
    } 
    delete[]requestedData; 
    return (result==TRUE); 

} 

bool WinDeviceHelper::getVendorAndDeviceIds(HDEVINFO hDevInfoSet,SP_DEVINFO_DATA &devInfo,QString& vendorId, QString& deviceId) 
{ 
    QString dataStr; 
    if(getDeviceRegistryString(hDevInfoSet,devInfo,SPDRP_HARDWAREID,dataStr)) 
    { 
     dataStr=dataStr.toUpper(); 
     QRegExp vidpid("(VID_)[0-9A-F]{4}(&PID_)[0-9A-F]{4}"); 
     int pos=dataStr.indexOf(vidpid); 
     if(pos>=0) 
     { 
      vendorId=dataStr.mid(pos+4,4); 
      pos+=8; 
      pos+=5; 
      deviceId=dataStr.mid(pos,4); 
      return true; 
     } 
    } 

    return false; 
} 

一塊使用

DWORD dwGuids = 0; 
    HDEVINFO hDevInfoSet = getDevInfoForClass(drvClass,dwGuids); 
    //Logger::logTrace(QString("WinDeviceHelper::searchForPort() found %1 port drivers for type %2").arg(dwGuids).arg(drvClass)); 
    if(dwGuids) 
    { 
     BOOL bMoreItems = TRUE; 
     int nIndex = 0; 

     SP_DEVINFO_DATA devInfo; 
     devInfo.cbSize = sizeof(SP_DEVINFO_DATA); 

     while(SetupDiEnumDeviceInfo(hDevInfoSet, nIndex, &devInfo) && (nIndex != -1)) 
     { 
      //Logger::logTrace(QString("WinDeviceHelper::searchForPort() enumerating ports. current index: %1").arg(nIndex)); 
      QString iVid,iPid; 
      QString fName; 

      if(getVendorAndDeviceIds(hDevInfoSet,devInfo,iVid,iPid) 
       enter code here 

     } 
    } 
+0

此外,是否有微軟鏈接解釋每個屬性意味着什麼(詳細)? – 2015-09-03 05:13:56

+1

谷歌屬性的名稱。是的,他們在msdn中,幫助使用它們的函數。但不是所有的函數都是這樣用常數名稱搜索,而不是按函數名稱搜索。 – Raiv 2015-09-03 14:44:39

相關問題