Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

winapi - How to get device properties in windows in c++?

In Windows, if I open Device Manager-> right click on the device -> Properties -> Details, I get {Property, Value} pairs. I want to access them in my C++ code in Visual Studio. How do I get it?

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try SetupDi_ functions,look here for example.

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

piece of usage

    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

        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...