c++winapisetupapi

Converting DriverVersion to Human readable format


I have searched high and low for it, could not find any documentation, I am able to get the DriverVersion as described Here. Creating and enumerating the device drives is working so there is no need to look there. The DriverVersion is a type of "DWORDLONG". I need to convert this to Human readable format like 20.xx.xx.xx. There ain't any documentation on MSDN, or anywhere I have searched.

Any help will be appreciated.

Example value of "DriverVersion" : 1688863374327808

Code snippet, if at all its required,

SP_DEVINFO_DATA devInfo;
                devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
                if (SetupDiEnumDeviceInfo(handle, 0, &devInfo))
                {   
                    if(SetupDiBuildDriverInfoList(handle,&devInfo,SPDIT_COMPATDRIVER))
                     {
                         SP_DRVINFO_DATA drvInfo;
                        drvInfo.cbSize = sizeof(SP_DRVINFO_DATA);
                            int i=0;
                        while(1)
                        {
                            try
                            {
                                if (SetupDiEnumDriverInfo(handle, &devInfo, SPDIT_COMPATDRIVER, i++, &drvInfo))
                                {
                                    cout<<"Version :"<<drvInfo.DriverVersion<<endl; // Need Human Readable version here.
                                }
                                else
                                    break;
                            }
                            catch(std::exception ex)
                            {
                                break;
                            }
                        }
                     }
                } 

Solution

  • You could use a ULARGE_INTEGER union, the HIWORD/LOWORD macros and string formatting utils like boost::format to do the following (untested code):

    SP_DRVINFO_DATA driverInfoData;
    ULARGE_INTEGER version;
    
    version.QuadPart = driverInfoData.DriverVersion;
    
    std::string versionStr = (boost::format("%s.%s.%s.%s")
        % HIWORD(version.HighPart)
        % LOWORD(version.HighPart)
        % HIWORD(version.LowPart)
        % LOWORD(version.LowPart)).str();
    

    Following your code and to get rid of boost simply do:

    std::cout << "Version: "
              << std::to_string(HIWORD(version.HighPart)) << "."
              << std::to_string(LOWORD(version.HighPart)) << "."
              << std::to_string(HIWORD(version.LowPart)) << "."
              << std::to_string(LOWORD(version.LowPart)) << std::endl;