如果打印机有问题,Windows的设备管理器会在有问题的打印机旁边显示一个特殊图标。
我正在使用DelphiXE2,我想知道如何在WindowsXP或更高版本中以编程方式检测这些信息。

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;

最佳答案

您可以使用Win32_PnPEntitywmi类和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.

关于windows - 检测设备(打印机,扫描仪,鼠标等)是否不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24067988/

10-12 17:06