Question

How do get VGA BUS type (AGP, PCI, PCI-e...) via VB.net?

This return what videocards in computer: SELECT Name, PNPDeviceID FROM Win32_VideoController

How can I get the bus type from these video cards to have PCI or PCI-e or AGP connected to computer?

Was it helpful?

Solution 2

Not the best solution, but it returns which VGA is in which port, which shows whether it is PCI or PCIe, and I also think if AGP, but I can't test this because I no longer have an AGP motherboard.

    Function GetName(ByVal devid As String) As String
        Dim sql As String = "SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE """ & devid.Replace("\", "\\") & """"
        Dim videoControllers As New ManagementObjectSearcher("root\CIMV2", sql)
        For Each controllerObj As ManagementObject In videoControllers.Get()
            Return controllerObj.Properties("Name").Value.ToString()
        Next
        Return devid
    End Function
    Function GetParent(ByVal devid As String) As String
        Dim sql As String = "SELECT * FROM Win32_PnPEntity WHERE DeviceID = """ & devid.Replace("\", "\\") & """"
        Dim MObjS As New ManagementObjectSearcher("root\CIMV2", sql)
        For Each MObj As ManagementObject In MObjS.Get()
            Dim outParams As ManagementBaseObject = MObj.InvokeMethod("GetDeviceProperties", Nothing, Nothing)
            For Each r In outParams.Properties("deviceProperties").Value
                If r.Properties("KeyName").Value = "DEVPKEY_Device_Parent" Then
                    Return GetName(r.Properties("Data").Value)
                End If
            Next
        Next
        Return devid
    End Function
    Sub GetGPUs()
        For Each MObj As ManagementObject In getManagementObjects("Win32_VideoController")
            Dim name As String = MObj.Properties("Name").Value.ToString()
            Dim PNPDeviceID As String = MObj.Properties("PNPDeviceID").Value.ToString()
            Debug.WriteLine(name & " - " & GetParent(PNPDeviceID))
        Next
    End Sub

Result is:

   NVIDIA GeForce GTX 1060 6GB - Intel(R) Xeon(R) E3 - 1200/1500 v5/6th Gen Intel(R) Core(TM) PCIe Controller (x16) - 1901

OTHER TIPS

You can use WMI to get this information. I used the code below. You must add a reference to System.Management. This code is pretty brittle, but it shows that the information is available using WMI. Look at the documentation on MSDN for other WMI classes that might be of interest to you.

Private Shared Sub Main()
    Dim videoControllers As ManagementObjectCollection = getManagementObjects("Win32_VideoController")

    For Each controllerObj As ManagementObject in videoControllers
        Dim pnpDeviceID As String = Path.GetFileName(controllerObj.Properties("PNPDeviceID").Value.ToString())
        Dim deviceBus As String = getDeviceBus(pnpDeviceID)
        Dim busType As String = getBusType(deviceBus)

        Console.WriteLine("{0}:  {1}", controllerObj.Properties("Name").Value, busType)
    Next
End Sub

Private Shared Function getManagementObjects(ByVal wmiClass As String) As ManagementObjectCollection 
    Using searcher As ManagementObjectSearcher = New ManagementObjectSearcher(String.Format("select * from {0}", wmiClass))
        Return searcher.Get()
    End Using
End Function

Private Shared Function getDeviceBus(ByVal pnpDeviceID As String) As String 

    Dim result As String = Nothing
    Dim coll As ManagementObjectCollection = getManagementObjects("Win32_DeviceBus")

    For Each mobj As ManagementObject In coll
        For Each props As PropertyData in mobj.Properties
            If props.Name = "Dependent" AndAlso props.Value.ToString().Contains(pnpDeviceID) Then
                result = mobj.Properties("Antecedent").Value.ToString().Split("="c)(1).Replace("""", "")
                Exit For
            End If
        Next
    Next

    Return result
End Function

Private Shared Function getBusType(ByVal deviceBus As String) As String 
    Dim busTypes As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
    busTypes.Add(-1, "Undefined")
    busTypes.Add(0, "Internal")
    busTypes.Add(1, "ISA")
    busTypes.Add(2, "EISA")
    busTypes.Add(3, "MicroChannel")
    busTypes.Add(4, "TurboChannel")
    busTypes.Add(5, "PCI Bus")
    busTypes.Add(6, "VME Bus")
    busTypes.Add(7, "NuBus")
    busTypes.Add(8, "PCMCIA Bus")
    busTypes.Add(9, "C Bus")
    busTypes.Add(10, "MPI Bus")
    busTypes.Add(11, "MPSA Bus")
    busTypes.Add(12, "Internal Processor")
    busTypes.Add(13, "Internal Power Bus")
    busTypes.Add(14, "PNP ISA Bus")
    busTypes.Add(15, "PNP Bus")
    busTypes.Add(16, "Maximum Interface Type")

    Dim result As String = Nothing
    Dim coll As ManagementObjectCollection = getManagementObjects("Win32_Bus")

    Dim busType As Integer = -1

    For Each mobj As ManagementObject in coll
        If mobj.Properties("DeviceID").Value.ToString() = deviceBus Then
            Integer.TryParse(mobj.Properties("BusType").Value.ToString(), busType)
            Exit For
        End If
    Next

    result = busTypes(busType)

    Return result
End Function

Which produces this result on my box:

ConfigMgr Remote Control Driver:  PCI Bus
NVIDIA GeForce 8400 GS    :  PCI Bus
Winvnc video hook driver:  PCI Bus
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top