WMI找不到所有设备

时间:2015-06-29 17:15:03

标签: vb.net wmi wmi-query

我尝试创建一个类来查看是否安装了设备驱动程序,如果没有设置errorlevel,那么bat文件可以使用" dpinst.exe"安装它。这样就会有一个完全静默的安装。我正在使用WMI读取实体(" Select * from Win32_PnPEntity")和Devices("" Select * from Win32_PnPDevice")。如果安装了相关设备,它会找到它,但如果没有,它就不会显示。如果我使用"计算机管理"它显示在"其他设备" Snippet showing what I mean
代码来运用类(主模块)....

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吗?

1 个答案:

答案 0 :(得分:0)

这篇文章可以提供帮助:http://www.msfn.orgboardtopic52741-install-unknown-devices

作者建议使用devcon.exe程序查找所有未知设备。

https://msdn.microsoft.com/windows/hardware/drivers/devtest/devcon