Suraj S
Suraj S

Reputation: 1019

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

Upvotes: 0

Views: 452

Answers (2)

fortytwo
fortytwo

Reputation: 531

If anyone's interested in the Golang implementation, here you go.

highPart := uint32(drvInfo.DriverVersion >> 32)
lowPart := uint32(drvInfo.DriverVersion & 0xffffffff)
versionStr := fmt.Sprintf("%d.%d.%d.%d",
    uint16(highPart>>16),
    uint16(highPart),
    uint16(lowPart>>16),
    uint16(lowPart))
fmt.Printf("Version: %s\n", versionStr)

Upvotes: 0

Wum
Wum

Reputation: 306

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;

Upvotes: 2

Related Questions