我的任务是找到一种方法来检测有故障或丢失的驱动程序,以便我的程序可以安装一个更好的适用于我的系统的程序。我一直在尝试的事情包括使用setupapi.dll
从注册表中获取值并尝试找到一些标志,让我知道驱动程序存在问题。在MSDN上,我发现“其他设备”的类 GUID 是4d36e97e-e325-11ce-bfc1-08002be10318
. 但是,如果我尝试枚举这些设备,我什么也得不到,即使我在设备管理器下看到该类别的设备。是否有其他 Windows API 可以让我通过某种方式识别此类设备或一般的驱动程序错误?我的代码如下所示:
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();
}
}
}
我也尝试过使用SetupDiGetDeviceRegistryProperty
来获取特定属性,但对于很多设备而言,并非所有属性都存在。我希望能够拨打这个电话并让它返回SPDRP_INSTALL_STATE
,但我还没有收到这个电话的实际回复。