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.