Ok, so here’s where I’m at now. This works for all the cases I’ve come across, anyway.
My root problem is detecting whether to update hardware drivers, in this case for an NVidia card. I attack this by traversing the HKLM\system\CurrentControlSet\Control\Enum key, finding devices with a HardwareID or CompatibleIDs entry matching one of the PnP Device IDs retrieved from the driver’s INF. Then, I check the corresponding keys beneath HKLM\System\CurrentlSet\Control\Class[ClassGUID] to see whether the matching device exists, has the correct ProviderName, and has a version equal to or higher than the one I propose to install.
I had working relevance for this some time ago, but had a whammy thrown at me – systems with multiple NVidia cards installed, of different models; where one model is supported by the new driver, but the old model is not supported by the new driver, installing the driver leaves the machine in an unbootable state. So now my logic has to be “there’s a matching card AND there’s not another NVidia card that doesn’t match the driver’s PnP Device List”.
Here’s what I’ve come up with. In reality the PnP device list for this one is 413 items long, I’ve snipped the list here but kept the evalution times for the full set:
Test 1 - NVidia Geforce driver is already up-to-date
q: exists (/* 0 = bus */ "PCI", /* 1 = ClassGUID */ "{4D36E968-E325-11CE-BFC1-08002BE10318}" as uppercase, /* 2 = Provider */ "NVIDIA" , /* 3 = Service */ "nvlddmkm", /* 4 = Version */ "23.21.13.9103", /* 5 = PnP list */ set of ((/* DEVICE LIST HERE */ "pci\ven_10de&dev_06d1";"pci\ven_10de&dev_06d1&subsys_077110de";"pci\ven_10de&dev_06d1&subsys_077210de") as lowercase ), /* 6 = Enum */ keys of keys of keys of key ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum") of native registry ) whose (( pathname of item 6 of it as string as lowercase starts with ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" as lowercase & item 0 of it as lowercase) AND /* if class is defined then class matches expectation (in this case, "Display"); if no class is listed then its an unknown device and may still be relevant */(not exists (values "ClassGUID" of item 6 of it, item 1 of it) whose (item 0 of it as string as uppercase != item 1 of it)) AND ( /* exists a hardware ID matching our expected list from INF*/ size of intersection of (set of (substrings separated by "%00" whose (it !="" ) of (values ("HardwareID";"CompatibleIDs") of item 6 of it as string as lowercase as trimmed string)) ; item 5 of it) > 0) AND /* if there is no "Driver" key then true, else check the driver present in the "Driver" key */ (if not exists value "Driver" of item 6 of it then true else (not exists (/* 0 = ENUM */ item 6 of it, /* 1 = Class key */ keys of keys of key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class" of native registry, /* 2 = Version */ item 4 of it, /* 3 = Provider */ item 2 of it) whose (pathname of item 1 of it as lowercase ends with value "Driver" of item 0 of it as string as lowercase AND ( /* wrong Provider */ value "ProviderName" of item 1 of it = item 3 of it and value "DriverVersion" of item 1 of it as string as version >= version /* VERSION HERE */ (item 2 of it))))) ) AND (not exists (keys of keys of keys of key ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum") of native registry, item 3 of it, item 5 of it) whose (value "Service" of item 0 of it as string as lowercase = item 1 of it as lowercase and size of intersection of (set of (substrings separated by "%00" whose (it !="" ) of (values ("HardwareID";"CompatibleIDs") of item 0 of it as string as lowercase as trimmed string)) ; item 2 of it) = 0) ))
A: False
T: 21.650 ms
I: singular boolean
Test 2 - coerce the driver version number to make it relevant
q: exists (/* 0 = bus */ "PCI", /* 1 = ClassGUID */ "{4D36E968-E325-11CE-BFC1-08002BE10318}" as uppercase, /* 2 = Provider */ "NVIDIA" , /* 3 = Service */ "nvlddmkm", /* 4 = Version */ "923.21.13.9103", /* 5 = PnP list */ set of ((/* DEVICE LIST HERE */ "pci\ven_10de&dev_06d1";"pci\ven_10de&dev_06d1&subsys_077110de";"pci\ven_10de&dev_06d1&subsys_077210de") as lowercase ), /* 6 = Enum */ keys of keys of keys of key ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum") of native registry ) whose (( pathname of item 6 of it as string as lowercase starts with ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" as lowercase & item 0 of it as lowercase) AND /* if class is defined then class matches expectation (in this case, "Display"); if no class is listed then its an unknown device and may still be relevant */(not exists (values "ClassGUID" of item 6 of it, item 1 of it) whose (item 0 of it as string as uppercase != item 1 of it)) AND ( /* exists a hardware ID matching our expected list from INF*/ size of intersection of (set of (substrings separated by "%00" whose (it !="" ) of (values ("HardwareID";"CompatibleIDs") of item 6 of it as string as lowercase as trimmed string)) ; item 5 of it) > 0) AND /* if there is no "Driver" key then true, else check the driver present in the "Driver" key */ (if not exists value "Driver" of item 6 of it then true else (not exists (/* 0 = ENUM */ item 6 of it, /* 1 = Class key */ keys of keys of key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class" of native registry, /* 2 = Version */ item 4 of it, /* 3 = Provider */ item 2 of it) whose (pathname of item 1 of it as lowercase ends with value "Driver" of item 0 of it as string as lowercase AND ( /* wrong Provider */ value "ProviderName" of item 1 of it = item 3 of it and value "DriverVersion" of item 1 of it as string as version >= version /* VERSION HERE */ (item 2 of it))))) ) AND (not exists (keys of keys of keys of key ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum") of native registry, item 3 of it, item 5 of it) whose (value "Service" of item 0 of it as string as lowercase = item 1 of it as lowercase and size of intersection of (set of (substrings separated by "%00" whose (it !="" ) of (values ("HardwareID";"CompatibleIDs") of item 0 of it as string as lowercase as trimmed string)) ; item 2 of it) = 0) ))
A: True
T: 30.507 ms
I: singular boolean