0

I tried creating a class that will see if a device driver is installed, if not set an errorlevel so that a bat file can use "dpinst.exe" to install it. This is so there will be a totally silent install. I am using WMI to read both the Entity ("Select * from Win32_PnPEntity") and Devices (""Select * from Win32_PnPDevice"). If the device in question is installed, it finds it, but if not it does not show up. If I use "Computer Management" it shows up under "Other Devices" Snippet showing what I mean
code to exercise the class (main module)....

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 & ")")

Class code snippet....

#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

Here is snippet output....

> 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

When I tried to post it I ran out of characters, how is the proper etiquette for this? Should I post it to a web site and post the URL for the output and code?

penright
  • 163
  • 4
  • 12
  • We do not need the entire output; perhaps the relevant portion. But what is the programming related question? Are you looking for a specific device? – Ňɏssa Pøngjǣrdenlarp Jun 29 '15 at 17:56
  • How does the "Computer Management" know that the TM-T88IV is a other device with a !? I have seen this style of question all over google, but I have not seen any answers. I assumed "CM" used WMI, but that was just a guess. If I install the driver in "CM" and my output it shows up under the "USB Root Hub". – penright Jun 29 '15 at 18:14

0 Answers0