2015-06-29 2 views
0

Я попытался создать класс, который будет видеть, установлен ли драйвер устройства, если не установлен уровень ошибок, чтобы файл bat мог использовать «dpinst.exe» для его установки. Это значит, что будет абсолютно бесшумная установка. Я использую WMI для чтения как Entity («Выбрать * из Win32_PnPEntity»), так и «Устройства» («« Выберите * из Win32_PnPDevice »). Если это устройство установлено, оно находит его, но если оно не отображается. Если я использую «Управление компьютером» он показывает в разделе «Другие устройства» Snippet showing what I mean
кода осуществлять класс (основной модуль) ....WMI не находит всех устройств

LogIt.Log("Started " & Command()) 
DispThis("Started " & Command() & NewLine) 
'TODO: add your program code here 
'LoadTypeLib() 
Using tmp As New clsWMI.PNPInfo("") 
    For xLoop As Integer = 0 To tmp.DeviceByClass.Count - 1 
     Dim tmpClass As String = tmp.DeviceByClass.Keys(xLoop) 
     DispThis(tmpClass & NewLine) 
     For yLoop As Integer = 0 To tmp.DeviceByClass(tmpClass).Count - 1 
      DispThis("  " & tmp.DeviceByClass(tmpClass).Item(yLoop).Name & NewLine) 
     Next 
    Next 
    DispThis("List of Devices and some info." & NewLine) 
    For xLoop As Integer = 0 To tmp.PNPDevices.Count - 1 
     DispThis(tmp.PNPDevices(xLoop).Caption & NewLine) 
     DispThis("    LastErrorCode: " & tmp.PNPDevices(xLoop).LastErrorCode & NewLine) 
     DispThis("    Manufacturer: " & tmp.PNPDevices(xLoop).Manufacturer & NewLine) 
     DispThis("      Name: " & tmp.PNPDevices(xLoop).Name & NewLine) 
     DispThis("     StatusInfo: " & tmp.PNPDevices(xLoop).StatusInfo & NewLine) 
     DispThis("  ConfigManagerErrorCode: " & tmp.PNPDevices(xLoop).ConfigManagerErrorCode & NewLine) 
    Next 
End Using 
DispThis("Finish (" & Environment.ExitCode & ")") 

код класса сниппет ....

#Region " ~ Plug and Play ~ " 
     Class PNPInfo 
      Implements IDisposable 

      Public PNPDevices As New List(Of PNPInfo_Device) 
      Public PNPClass As New List(Of PNPInfo_ClassByDevice) 
      Public DeviceByClass As New Dictionary(Of String, List(Of PNPInfo_Device)) 
      Sub New(ByVal pComputerName As String, Optional ByVal pUserName As String = Nothing, Optional ByVal pPassword As String = Nothing) 
       Dim tmpDevices As ManagementObjectCollection = Nothing 
       Dim tmpDevicesByClass As ManagementObjectCollection = Nothing 
       Using wmi As New clsWMI(pComputerName, pUserName, pPassword) 
        tmpDevices = wmi.ReturnManagementObject("Select * from Win32_PnPEntity") 
        tmpDevicesByClass = wmi.ReturnManagementObject("Select * from Win32_PnPDevice") 
       End Using 
       For xLoop As Integer = 0 To tmpDevices.Count - 1 
        Dim tmpDevice As New PNPInfo_Device(tmpDevices(xLoop)) 
        PNPDevices.Add(tmpDevice) 
       Next 
       For xLoop As Integer = 0 To tmpDevicesByClass.Count - 1 
        Dim tmpDeviceByClass As New PNPInfo_ClassByDevice(tmpDevicesByClass(xLoop)) 
        PNPClass.Add(tmpDeviceByClass) 
       Next 
       For xLoop As Integer = 0 To PNPClass.Count - 1 
        For yLoop As Integer = 0 To PNPDevices.Count - 1 
         'If PNPDevices(yLoop).PNPDeviceID.StartsWith("HDAUDIO") OrElse PNPDevices(yLoop).PNPDeviceID.StartsWith("FUNC_01") Then Stop 
         If PNPClass(xLoop).SystemElement = PNPDevices(yLoop).PNPDeviceID Then 
          If Not DeviceByClass.ContainsKey(PNPClass(xLoop).SameElement) Then 
           DeviceByClass.Add(PNPClass(xLoop).SameElement, New List(Of PNPInfo_Device)) 
          End If 
          DeviceByClass(PNPClass(xLoop).SameElement).Add(PNPDevices(yLoop)) 
          Exit For 
         End If 
        Next 
       Next 
      End Sub 

#Region "IDisposable Support" 
      Private disposedValue As Boolean ' To detect redundant calls 

      ' IDisposable 
      Protected Overridable Sub Dispose(ByVal disposing As Boolean) 
       If Not Me.disposedValue Then 
        If disposing Then 
         ' TODO: dispose managed state (managed objects). 
        End If 

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. 
        ' TODO: set large fields to null. 
       End If 
       Me.disposedValue = True 
      End Sub 

      ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources. 
      'Protected Overrides Sub Finalize() 
      ' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. 
      ' Dispose(False) 
      ' MyBase.Finalize() 
      'End Sub 

      ' This code added by Visual Basic to correctly implement the disposable pattern. 
      Public Sub Dispose() Implements IDisposable.Dispose 
       ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. 
       Dispose(True) 
       GC.SuppressFinalize(Me) 
      End Sub 
#End Region 

     End Class 
     Class PNPInfo_ClassByDevice 
      Public SameElement As String 
      Public SystemElement As String 
      Sub New(ByVal pobjMgmt As ManagementObject) 
       SameElement = CType(pobjMgmt("SameElement"), String) 
       SystemElement = CType(pobjMgmt("SystemElement"), String) 
       If SameElement Is Nothing OrElse SystemElement Is Nothing Then 
        Throw New Exception("Management object was not parsed right." & NewLine & "SameElement: '" & CStrNull(SameElement) & "'" & NewLine & "SystemElement: '" & CStrNull(SystemElement) & "'" & NewLine) 
       End If 
       Dim tmpStart As Integer = 0 
       Dim tmpEnd As Integer = 0 
       tmpStart = SameElement.IndexOf(":Win32_") + 7 
       tmpEnd = SameElement.IndexOf(".DeviceID", tmpStart) 
       SameElement = SameElement.Substring(tmpStart, tmpEnd - tmpStart) 
       tmpStart = SystemElement.IndexOf(".DeviceID=") + 11 
       tmpEnd = SystemElement.Length - 1 
       SystemElement = SystemElement.Substring(tmpStart, tmpEnd - tmpStart).Replace("\\", "\") 
      End Sub 
     End Class 
     Class PNPInfo_Device 
      public Availability As Integer 
      public Caption As String 
      public ClassGuid As String 
      public CompatibleID() As String 
      public ConfigManagerErrorCode As Integer 
      public ConfigManagerUserConfig As Boolean 
      public CreationClassName As String 
      public Description As String 
      public DeviceID As String 
      public ErrorCleared As Boolean 
      public ErrorDescription As String 
      public HardwareID() As String 
      public InstallDate As DateTime 
      public LastErrorCode As Integer 
      public Manufacturer As String 
      public Name As String 
      public PNPDeviceID As String 
      public PowerManagementCapabilities() As Integer 
      public PowerManagementSupported As Boolean 
      public Service As String 
      public Status As String 
      public StatusInfo As String 
      public SystemCreationClassName As String 
      public SystemName As String 
      Sub New(ByVal pobjMgmt As ManagementObject) 
       Availability = CType(GetPropObj(pobjMgmt, "Availability"), Integer) 
       Caption = CType(GetPropObj(pobjMgmt, "Caption"), String) 
       ClassGuid = CType(GetPropObj(pobjMgmt, "ClassGuid"), String) 
       CompatibleID = CType(GetPropObj(pobjMgmt, "CompatibleID"), String()) 
       ConfigManagerErrorCode = CType(GetPropObj(pobjMgmt, "ConfigManagerErrorCode"), Integer) 
       ConfigManagerUserConfig = CType(GetPropObj(pobjMgmt, "ConfigManagerUserConfig"), Boolean) 
       CreationClassName = CType(GetPropObj(pobjMgmt, "CreationClassName"), String) 
       Description = CType(GetPropObj(pobjMgmt, "Description"), String) 
       DeviceID = CType(GetPropObj(pobjMgmt, "DeviceID"), String) 
       ErrorCleared = CType(GetPropObj(pobjMgmt, "ErrorCleared"), Boolean) 
       ErrorDescription = CType(GetPropObj(pobjMgmt, "ErrorDescription"), String) 
       HardwareID = CType(GetPropObj(pobjMgmt, "HardwareID"), String()) 
       InstallDate = CType(GetPropObj(pobjMgmt, "InstallDate"), DateTime) 
       LastErrorCode = CType(GetPropObj(pobjMgmt, "LastErrorCode"), Integer) 
       Manufacturer = CType(GetPropObj(pobjMgmt, "Manufacturer"), String) 
       Name = CType(GetPropObj(pobjMgmt, "Name"), String) 
       PNPDeviceID = CType(GetPropObj(pobjMgmt, "PNPDeviceID"), String) 
       PowerManagementCapabilities = CType(GetPropObj(pobjMgmt, "PowerManagementCapabilities"), Integer()) 
       PowerManagementSupported = CType(GetPropObj(pobjMgmt, "PowerManagementSupported"), Boolean) 
       Service = CType(GetPropObj(pobjMgmt, "Service"), String) 
       Status = CType(GetPropObj(pobjMgmt, "Status"), String) 
       StatusInfo = CType(GetPropObj(pobjMgmt, "StatusInfo"), String) 
       SystemCreationClassName = CType(GetPropObj(pobjMgmt, "SystemCreationClassName"), String) 
       SystemName = CType(GetPropObj(pobjMgmt, "SystemName"), String) 
      End Sub 
      Private Function GetPropObj(ByVal pMgmt As ManagementObject, ByVal pProperty As String) As Object 
       Dim retObject As Object = Nothing 
       Try 
        retObject = pMgmt(pProperty) 
       Catch ex As Exception 

       End Try 
       Return retObject 
      End Function 
     End Class 

     Public Function ReturnManagementObject(ByVal pWMIString As String) As ManagementObjectCollection 
      Dim tmpObj As ManagementObjectSearcher = New ManagementObjectSearcher(Cimv2Scope, New ObjectQuery(pWMIString)) 
      Return tmpObj.Get 
     End Function 
#End Region 

Этот снимок вывода ...

> Started Bus 
>  PCI bus DiskDrive 
>  Hitachi HTS543232A7A384 SoundDevice 
>  Realtek High Definition Audio IDEController 
>  Intel(R) 6 Series/C200 Series Chipset Family 4 port Serial ATA Storage Controller - 1C00 
>  Primary IDE Channel 
>  Secondary IDE Channel 
>  Intel(R) 6 Series/C200 Series Chipset Family 2 port Serial ATA Storage Controller - 1C08 
>  Primary IDE Channel 
>  Secondary IDE Channel USBHub 
>  USB Root Hub 
>  Generic USB Hub 
>  USB Root Hub 
>  USB Root Hub 
>  Generic USB Hub 
>  USB Composite Device Keyboard 
>  USB Human Interface Device 
>  USB Human Interface Device USBController 
>  Intel(R) 6 Series/C200 Series Chipset Family USB Enhanced Host Controller - 1C2D 
>  ASMedia XHCI Controller 
>  Intel(R) 6 Series/C200 Series Chipset Family USB Enhanced Host Controller - 1C26 DesktopMonitor 
>  Plug and Play Monitor VideoController 
>  Intel(R) HD Graphics PointingDevice 
>  3M MicroTouch Serial Sensor SerialPort 
>  Communications Port (COM1) 
>  Communications Port (COM2) 
>  Communications Port (COM3) 
>  Communications Port (COM4) 
>  Communications Port (COM5) 
>  Communications Port (COM10) 
>  Communications Port (COM7) 
>  Communications Port (COM8) 
>  Communications Port (COM9) 
>  Communications Port (COM6) NetworkAdapter 
>  Intel(R) PRO/100 VE Network Connection - Packet Scheduler Miniport 
>  WAN Miniport (L2TP) 
>  WAN Miniport (PPTP) 
>  WAN Miniport (PPPOE) 
>  Direct Parallel 
>  WAN Miniport (IP) 
>  WAN Miniport (IP) - Packet Scheduler Miniport 
>  Broadcom NetXtreme Gigabit Ethernet - Packet Scheduler Miniport 
>  Intel(R) 82579LM Gigabit Network Connection 
>  Intel(R) 82579LM Gigabit Network Connection - Packet Scheduler Miniport 

Когда я попытался опубликовать его, у меня кончились персонажи, как правильно этический этикет? Должен ли я публиковать его на веб-сайте и размещать URL-адрес для вывода и кода?

+0

Нам не нужен весь выход; возможно, соответствующую часть. Но каков вопрос, связанный с программированием? Вы ищете конкретное устройство? – Plutonix

+0

Как «Управление компьютером» знает, что TM-T88IV - это другое устройство с! Я видел этот стиль вопроса по всему Google, но я не видел никаких ответов. Я предположил, что «CM» использовал WMI, но это было всего лишь предположение. Если я установлю драйвер в «CM», а мой вывод появится в разделе «USB Root Hub». – penright

ответ

Смежные вопросы