I am tasked with finding a way to detect a faulty or missing driver so that my program can then install a better one that will work with my system. Things I have been trying include using setupapi.dll to get values from the Registry and try to find some flag that will let me know there is a problem with the driver. On MSDN I found that the Class GUID for "Other Devices" is 4d36e97e-e325-11ce-bfc1-08002be10318. However if I try to enumerate those devices I get nothing, even if I see devices in that category under the device manager. Is there another Windows API that could give me some way of identifying devices in this category or driver errors in general? My code looks like:
static void Main(string[] args)
{
Guid displayClass = new Guid("4d36e97e-e325-11ce-bfc1-08002be10318");
SafeDevInfoHandle hDevInfo = NativeMethods.SetupDiGetClassDevs(ref displayClass,
null, IntPtr.Zero, DIGetClassFlags.DIGCF_PRESENT);
if (hDevInfo.IsInvalid)
throw new Win32Exception();
DevInfoData did = new DevInfoData();
did.size = Marshal.SizeOf(did);
for (uint i = 0; NativeMethods.SetupDiEnumDeviceInfo(hDevInfo, i, ref did); i++)
{
if (NativeMethods.SetupDiBuildDriverInfoList(hDevInfo, ref did, DriverType.SPDIT_COMPATDRIVER))
{
DriverInfoData drvData = new DriverInfoData();
drvData.Size = Marshal.SizeOf(drvData);
for (uint j = 0; NativeMethods.SetupDiEnumDriverInfo(hDevInfo, ref did, DriverType.SPDIT_COMPATDRIVER, j, ref drvData); j++)
{
Console.WriteLine(drvData.ToString());
}
}
else
{
throw new Win32Exception();
}
}
}
I've also tried using SetupDiGetDeviceRegistryProperty to get specific properties, but for a lot of devices not all properties are present. I would love to be able to make a call to this and have it return SPDRP_INSTALL_STATE but I've yet to receive an actual response with this call.
The documentation for SetupDiGetClassDevs contains sample code (example 5) for enumerating "Other Devices" and contains the following note:
You cannot set the ClassGuid parameter to GUID_DEVCLASS_UNKNOWN to detect devices with an unknown setup class. Instead, you must follow this example.
Looking up GUID_DEVCLASS_UNKNOWN in the DDK, it is the same GUID in your posted code. So the approach you were trying to use is documented to not work.
Instead, enumerate all devices using the DIGCF_ALLCLASSES option, and look up the device class for each one. According to the example code, for the devices you're interested in, looking up the device class will fail with ERROR_NOT_FOUND.
(Depending on what exactly you're trying to achieve, there may be other approaches that would work better, e.g., you could try looking up DEVPKEY_Device_ProblemCode for each device. I'm not really familiar with all of the possibilities.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With