6

I'm trying to get the version number of a driver programmatically. The seems to be done by using SetupDiEnumDriverInfo to get a SP_DRVINFO_DATA struct and inspect the DriverVersion field.

The following code works, but returns two different versions for the same driver. My device is a custom USB device, with a single .sys file. There is only one device connected to my machine. I specify DIGCF_PRESENT to only query the drivers of currently attached devices.

int main(void)
{
    // Get the "device info set" for our driver GUID
    HDEVINFO devInfoSet = SetupDiGetClassDevs(
                              &GUID_DEVINTERFACE_USBSPI, NULL, NULL,
                              DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    // Cycle through all devices currently present
    for (int i = 0; ; i++)
    {
        // Get the device info for this device
        SP_DEVINFO_DATA devInfo;
        devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
        if (!SetupDiEnumDeviceInfo(devInfoSet, i, &devInfo))
            break;

        // Build a list of driver info items that we will retrieve below
        if (!SetupDiBuildDriverInfoList(devInfoSet, 
                                        &devInfo, SPDIT_COMPATDRIVER))
            return -1; // Exit on error

        // Get all the info items for this driver 
        // (I don't understand why there is more than one)
        for (int j = 0; ; j++)
        {
            SP_DRVINFO_DATA drvInfo;
            drvInfo.cbSize = sizeof(SP_DRVINFO_DATA);
            if (!SetupDiEnumDriverInfo(devInfoSet, &devInfo, 
                                       SPDIT_COMPATDRIVER, j, &drvInfo))
                break;

            printf("Driver version is %08x %08x\n", 
                   (unsigned)(drvInfo.DriverVersion >> 32), 
                   (unsigned)(drvInfo.DriverVersion & 0xffffffffULL));
        }
    }

    SetupDiDestroyDeviceInfoList(devInfoSet);

    return 0;
}

On my machine this prints:

Driver version is 00000000 000015d3
Driver version is 00020004 00000000

On a friend's machine, it prints:

Driver version is 00020004 00000000
Driver version is 00020004 00000000

The second line matches the number reported by device manager.

Disclaimer: I previously asked a similar question. This is a new question about why SetupDiEnumDriverInfo returns more than one driver version.

3
  • 1
    Is it the outer or inner loop that produces two items? Does the code produce multiple results for any other USB devices? Have you tried using devcon findall * to check whether there are in fact two instances of the driver installed? Commented Apr 26, 2013 at 1:54
  • Sorry for the ambiguity - it is the inner loop that produces two items. Other devices produce multiple items too - there's a mixture of 1, 2 and 3. devcon findall * only finds a single instance of the driver installed. Anyway, you've given me a kick in a useful direction. I'll keep fiddling and probably update the question later today. Thanks. Commented Apr 26, 2013 at 10:40
  • For anyone using this code to get the driver versions of none-custom stuff, If the human readable version number that you can see in device manager is 4 parts the DriverVersion needs to be bit shifted into 4 parts like unsigned short part1 = (version >> 48) & 0xffff; unsigned short part2 = (version >> 32) & 0xffff; unsigned short part3 = (version >> 16) & 0xffff; unsigned short part4 = version & 0xffff;
    – pSquared
    Commented Mar 23, 2023 at 11:22

2 Answers 2

9

As your code is written, all the possible drivers will be output. Try doing the following to filter on only the installed driver:

SP_DEVINSTALL_PARAMS InstallParams;
if ( !SetupDiGetDeviceInstallParams( devInfoSet, &devInfo, &InstallParams ) )
{
   //Error
}
else
{
   InstallParams.FlagsEx |= DI_FLAGSEX_INSTALLEDDRIVER;
   if ( !SetupDiSetDeviceInstallParams( devInfoSet, &devInfo, &InstallParams) )
   {
      //Errror
   }
}

I found this at http://doxygen.reactos.org/df/db2/dll_2win32_2devmgr_2misc_8c_a1cd0b33c1785392a37689433dc99e482.html

0
1

I find the solution very complicated: SetupDiBuildDriverInfoList, SetupDiEnumDriverInfo, SetupDiGetDeviceInstallParams, SetupDiSetDeviceInstallParams.

There is another option to get the version of only the driver which is currently in use although there are multiple drivers installed.

With

SetupDiGetDeviceRegistryProperty(devInfoSet, &devInfo, SPDRP_DRIVER, 
                                 NULL, (BYTE*)UnicodeBuf, BufferSize, NULL);

I obtain the driver path in the registry which looks like this:

"{4D36E978-E325-11CE-BFC1-08002BE10318}\0000"

I load this into a variable s_DriverPath and then I read the driver version directly from HKEY_LOCAL_MACHINE:

CString s_RegPath = L"SYSTEM\\CurrentControlSet\\Control\\Class\\" + s_DriverPath;

Driver Info in Registry

The key "DriverVersion" returns the version of the currently used driver. When you update the driver to a newer version Windows adapts all the registry entries automatically. So this way you always get the currently used driver version.

There is more info which you can read about the driver. The entry "DriverDateData" are 8 bytes which contain the driver date as FILETIME. All this information comes from the INF file.

This works from Windows XP until Windows 10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.