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.
devcon findall *
to check whether there are in fact two instances of the driver installed?DriverVersion
needs to be bit shifted into 4 parts likeunsigned short part1 = (version >> 48) & 0xffff; unsigned short part2 = (version >> 32) & 0xffff; unsigned short part3 = (version >> 16) & 0xffff; unsigned short part4 = version & 0xffff;