Programmatically detect faulty or missing drivers

I have been tasked with finding a way to detect a faulty or missing driver so that my program can install the best one that will work with my system. Things I've tried include using setupapi.dll

to get values ​​from the registry and trying to find some kind of 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 list these devices, I get nothing, even if I see devices in that category under the Device Manager. Is there any other Windows API that can give me a way to identify devices in this category, or driver errors in general? My code looks like this:

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 also tried to use SetupDiGetDeviceRegistryProperty

to get certain properties, but not all properties are present for many devices. I would love to be able to call and return it SPDRP_INSTALL_STATE

, but I have not yet received an actual response with this call.

+3


source to share


1 answer


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 discover devices with an unknown configuration class. Instead, you should follow this example.

Picking up GUID_DEVCLASS_UNKNOWN

in the DDK, this is the same GUID in your posted code. So the approach you were trying to take is documented to not work.



Instead, list all devices using the parameter DIGCF_ALLCLASSES

and find the device class for each one. Based on the sample code for the devices of interest, searching for the device class will fail with ERROR_NOT_FOUND

.

(Depending on what exactly you are trying to achieve, there may be other approaches that work better, for example, you can try searching DEVPKEY_Device_ProblemCode

for each device. I'm not very familiar with all the possibilities.)

+2


source







All Articles