How to get driver version in Windows from C ++

I am looking for a software way to get the driver version number. I need the same number that the device manager shows in the driver properties for the device.

Reference Information. I have an application that speaks with some special equipment. The device driver for custom hardware detected errors up to a specific version number. I want the application to check the driver version and warn the user about the need to update it. The application runs on Windows XP and 7 and is written in C ++.

The previous hack I used was to read the .sys file directly from the system / drivers and directly look for "FileVersion". This is bad for many reasons. In particular, Windows 7 requires administrator privileges.

I know the class GUID and hardware identifier (for example, "USB \ VID_1234 & PID_5678").

Currently, the application uses SetupDiGetClassDevs, SetupDiEnumDeviceInterfaces, and then SetupDiGetDeviceInterfaceDetail to get "DevicePath". He then calls CreateFile with this path to talk to the driver.

It looks like I need to get the SP_DRVINFO_DATA structure from somewhere. I tried various functions from setupapi.h like SetupDiGetDeviceInterfaceDetail. Here is the code I tried to skip:

int main(void) { HDEVINFO DeviceInfoSet = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USBSPI, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); SP_INTERFACE_DEVICE_DATA InterfaceDeviceData; InterfaceDeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA); // Cycle through all devices. for (int i = 0; i < 32; i++) { if (!SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, (LPGUID)&GUID_DEVINTERFACE_USBSPI, i, &InterfaceDeviceData)) break; PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData; DWORD RequiredSize; SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, &InterfaceDeviceData, NULL, 0, &RequiredSize, NULL); DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY, RequiredSize); try { DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, &InterfaceDeviceData, DeviceInterfaceDetailData, RequiredSize, NULL, NULL); // Try to get the driver info. This part always fails with code // 259 (ERROR_NO_MORE_ITEMS). SP_DRVINFO_DATA drvInfo; drvInfo.cbSize = sizeof(SP_DRVINFO_DATA); if (!SetupDiEnumDriverInfo(DeviceInfoSet, NULL, SPDIT_CLASSDRIVER, i, &drvInfo)) printf("error = %d\n", GetLastError()); printf("Driver version is %08x %08x\n", drvInfo.DriverVersion >> 32, drvInfo.DriverVersion & 0xffffffff); } catch(...) { HeapFree(GetProcessHeap(), 0, DeviceInterfaceDetailData); throw; } HeapFree(GetProcessHeap(), 0, DeviceInterfaceDetailData); } SetupDiDestroyDeviceInfoList(DeviceInfoSet); return 0; } 

Change My updated code now looks like this:

 HDEVINFO devInfoSet = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USBSPI, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // Cycle through all devices. 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; // Get the first info item for this driver SP_DRVINFO_DATA drvInfo; drvInfo.cbSize = sizeof(SP_DRVINFO_DATA); if (!SetupDiEnumDriverInfo(devInfoSet, &devInfo, SPDIT_COMPATDRIVER, 0, &drvInfo)) printf("err - %d\n", GetLastError()); // Still fails with "no more items" } SetupDiDestroyDeviceInfoList(devInfoSet); 
+4
source share
1 answer

You are not using i properly as an index in SetupDiEnumDriverInfo . This should be an inner loop for each item of driver information for each driver. As a result, you will not be able to get information about driver No. 0 for device No. 1.

However, this does not explain why the # 0 information for device # 0 fails. To do this, you need to look at the second parameter SetupDiEnumDriverInfo . This is the SP_DEVINFO_DATA structure for your device, but you will leave it NULL . This gives you a list of drivers associated with the device class, not the device. That is, that works for mice and USB drives that have class drivers. Your device might have a vendor specific driver, so you need a driver for that particular device.

+3
source

All Articles