检测设备(打印机,扫描仪,鼠标等)是否无法正常工作

时间:2014-06-05 18:50:56

标签: windows delphi winapi delphi-xe2

如果打印机出现问题,Windows的设备管理器会在相关打印机旁边显示一个特殊图标。

我使用的是Delphi XE2,我想知道如何以编程方式检测Windows XP或更高版本的此类信息,即

function DetectDriversProblems() : TStringList;
begin
    // Scan Computer or query Windows to know if a driver has issues and
    // return list of faulty hardware (if any)
end;

1 个答案:

答案 0 :(得分:4)

您可以使用Win32_PnPEntity WMI类和ConfigManagerErrorCode属性。

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

  Procedure ListDevicesConfigError;
    const
      wbemFlagForwardOnly = $00000020;
    var
      FSWbemLocator : OLEVariant;
      FWMIService   : OLEVariant;
      FWbemObjectSet: OLEVariant;
      FWbemObject   : OLEVariant;
      oEnum         : IEnumvariant;
      iValue        : LongWord;
    begin;
      FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\cimv2', '', '');
      FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM  Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0','WQL',wbemFlagForwardOnly);
      oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      while oEnum.Next(1, FWbemObject, iValue) = 0 do
      begin
        Writeln(Format('Class GUID %s',[String(FWbemObject.ClassGUID)]));
        Writeln(Format('Description %s',[String(FWbemObject.Description)]));
        Writeln(Format('Device ID %s',[String(FWbemObject.DeviceID)]));
        Writeln(Format('Manufacturer %s',[String(FWbemObject.Manufacturer)]));
        Writeln(Format('Name %s',[String(FWbemObject.Name)]));
        Writeln(Format('PNP Device ID %s',[String(FWbemObject.PNPDeviceID)]));
        Writeln('');
        FWbemObject:=Unassigned;
      end;
    end;

begin
 try
    CoInitialize(nil);
    try
      ListDevicesConfigError;
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); 
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;      
end.