【问题标题】:How to get device properties in windows in c++?如何在 C++ 中获取 Windows 中的设备属性?
【发布时间】:2017-09-12 00:56:08
【问题描述】:

在 Windows 中,如果我打开设备管理器 -> 右键单击​​设备 -> 属性 -> 详细信息,我会得到 {Property, Value} 对。我想在 Visual Studio 的 C++ 代码中访问它们。如何获得?

谢谢,

【问题讨论】:

  • 您具体要寻找哪些物业?
  • @fardjad 我需要知道:匹配设备 ID。你能帮忙吗

标签: c++ winapi properties device


【解决方案1】:

尝试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

        }
    }

【讨论】:

  • 另外,是否有 Microsoft 链接解释每个属性的含义(详细)?
  • google 属性的名称。是的,它们在 msdn 中,在使用它们的功能的帮助中。但不是在所有函数中都按常量名搜索,而不是按函数名。
  • 链接已失效。
猜你喜欢
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2013-01-25
相关资源
最近更新 更多