Hide Hidden Devices

cornelvis

Active member
Joined
Jul 15, 2005
Messages
30
Programming Experience
3-5
Hi All,

Been a long time since my last thread but here we go.
I'm using VS.NET 2010 Prof Beta at the moment and I'm trying to list all Nic Cards and their settings into a text box. I succeeded with a script from the web (which had an error in the first place) and some adjustments to it.
But.....
It lists all hidden devices too (as to be shown in Device manager: Show hidden devices) I worked my way around it and let it jump to next if it doesn't have a IP address, but I find this a bit clumsy and want to really skip them if they are hidden and not just the way I did this now. Plus it still does show the same NiC (different name, same MAC#) in the list because it has an IP, but in Device manager this one is hidden.
Anyone can give me a hint on how to do this?

here is my code:

VB.NET:
Imports System.Management

Public Class Form1

    Public Function getNetworkAdapterStructure() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("AdapterType"))
        dt.Columns.Add(New DataColumn("MACAddress"))
        dt.Columns.Add(New DataColumn("Manufacturer"))
        Return dt
    End Function

    Public Sub addNetworkAdapterRow(ByRef dt As DataTable, ByVal AdapterType As String, ByVal MACAddress As String, ByVal Manufacturer As String)
        Dim dr As DataRow
        dr = dt.NewRow
        dr("AdapterType") = AdapterType
        dr("MACAddress") = MACAddress
        dr("Manufacturer") = Manufacturer
        dt.Rows.Add(dr)
    End Sub

    Private Sub btnExtended_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtended.Click
        TextBox1.Text = ""

        Try
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration")
            For Each queryObj As ManagementObject In searcher.Get()
                If queryObj("IPAddress") Is Nothing Then GoTo NotNeeded

                Me.TextBox1.Text &= "Caption: " + queryObj("Caption") & ControlChars.NewLine
                Me.TextBox1.Text &= "DHCPEnabled: " + (queryObj("DHCPEnabled").ToString) & ControlChars.NewLine
                Me.TextBox1.Text &= "DHCPServer: " + queryObj("DHCPServer") & ControlChars.NewLine
                Me.TextBox1.Text &= "DNSDomain: " + queryObj("DNSDomain") & ControlChars.NewLine
                Me.TextBox1.Text &= "DNSHostName: " + queryObj("DNSHostName") & ControlChars.NewLine

                If queryObj("IPAddress") Is Nothing Then
                    Me.TextBox1.Text &= "IPAddress: " + queryObj("IPAddress") & ControlChars.NewLine
                Else
                    Dim arrIPAddress As String()
                    arrIPAddress = queryObj("IPAddress")
                    For Each arrValue As String In arrIPAddress
                        Me.TextBox1.Text &= "IPAddress: {0}" + arrValue & ControlChars.NewLine
                    Next
                End If
                If queryObj("IPSubnet") Is Nothing Then
                    Me.TextBox1.Text &= "IPSubnet: " + queryObj("IPSubnet") & ControlChars.NewLine
                Else
                    Dim arrIPSubnet As String()
                    arrIPSubnet = queryObj("IPSubnet")
                    For Each arrValue As String In arrIPSubnet
                        Me.TextBox1.Text &= "IPSubnet: {0}" + arrValue & ControlChars.NewLine
                    Next
                End If
                Me.TextBox1.Text &= "MACAddress: {0}" + queryObj("MACAddress") & ControlChars.NewLine
                Me.TextBox1.Text &= "======================================" & ControlChars.NewLine
NotNeeded:  Next

        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying forWMI data: " & err.Message)
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        End
    End Sub
End Class

Thanks in advance
 
I think that list show Win32_NetworkAdapter where PhysicalAdapter=True.
 
From thread http://www.vbdotnetforums.com/articles-interest/13972-wmi-code-creator-1-0-a.html download and run that app, select VB.Net language. WCC can be used to test, query and get sample code for anything WMI related. Here's how you can do this specific query: From Query tab select the WMI class, select the properties you want included (for example Name and PhysicalAdapter), click the 'search property values' button and select 'Phys...=False' in list below. Now you have working sample code.

When you've found something useful in WMI I recommend you generate a strongly typed VB.Net class for the WMI class, as described in same thread. In this case the code I'd use would simply be:
VB.NET:
For Each adp As Win32.NetworkAdapter In Win32.NetworkAdapter.GetInstances
    If adp.PhysicalAdapter Then
        Console.WriteLine(adp.Name)
    End If
Next
...while it also here would be possible to limit the query:
VB.NET:
.GetInstances("PhysicalAdapter='True'")
 
Thanks for the quick replies, I will try it in a few minutes

Thanks for the point out to the tool!

Greetzzz,

CornElvis
 
From thread http://www.vbdotnetforums.com/articles-interest/13972-wmi-code-creator-1-0-a.html download and run that app, select VB.Net language. WCC can be used to test, query and get sample code for anything WMI related. Here's how you can do this specific query: From Query tab select the WMI class, select the properties you want included (for example Name and PhysicalAdapter), click the 'search property values' button and select 'Phys...=False' in list below. Now you have working sample code.

When you've found something useful in WMI I recommend you generate a strongly typed VB.Net class for the WMI class, as described in same thread. In this case the code I'd use would simply be:
VB.NET:
For Each adp As Win32.NetworkAdapter In Win32.NetworkAdapter.GetInstances
    If adp.PhysicalAdapter Then
        Console.WriteLine(adp.Name)
    End If
Next
...while it also here would be possible to limit the query:
VB.NET:
.GetInstances("PhysicalAdapter='True'")

Sorry to say, but I tried multiple things, but I just can't fit it into my code....
I'm not a very expreienced programmer and only program when I need tools and then start to dig into it.
Can you give me a better hint?
Or am I asking too much now?

I tried to put the code under a new button, but it keeps telling me that W32 is not defined. :confused:

thanks for the effort
 
Have you generated the VB.Net class for Win32_NetworkAdapter and added it to your project as described in other thread?
 
Have you generated the VB.Net class for Win32_NetworkAdapter and added it to your project as described in other thread?

As far as my knowlegde goes, yes.
I didn't update it yet to go into the textbox, because it already giving errors
I added another button called btnTest to run it from.

Here is the code I have for the main Form:


VB.NET:
Imports System.Management
Imports WindowsApplication1.Win32

Public Class Form1

    Public Function getNetworkAdapterStructure() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("AdapterType"))
        dt.Columns.Add(New DataColumn("MACAddress"))
        dt.Columns.Add(New DataColumn("Manufacturer"))
        Return dt
    End Function

    Public Sub addNetworkAdapterRow(ByRef dt As DataTable, ByVal AdapterType As String, ByVal MACAddress As String, ByVal Manufacturer As String)
        Dim dr As DataRow
        dr = dt.NewRow
        dr("AdapterType") = AdapterType
        dr("MACAddress") = MACAddress
        dr("Manufacturer") = Manufacturer
        dt.Rows.Add(dr)
    End Sub

    Private Sub btnExtended_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtended.Click
        TextBox1.Text = ""

        Try
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration ")


            For Each queryObj As ManagementObject In searcher.Get()

                'If queryObj("IPAddress") Is Nothing Then GoTo NotNeeded

                Me.TextBox1.Text &= "Caption: " + queryObj("Caption") & ControlChars.NewLine
                Me.TextBox1.Text &= "DHCPEnabled: " + (queryObj("DHCPEnabled").ToString) & ControlChars.NewLine
                Me.TextBox1.Text &= "DHCPServer: " + queryObj("DHCPServer") & ControlChars.NewLine
                Me.TextBox1.Text &= "DNSDomain: " + queryObj("DNSDomain") & ControlChars.NewLine
                Me.TextBox1.Text &= "DNSHostName: " + queryObj("DNSHostName") & ControlChars.NewLine

                If queryObj("IPAddress") Is Nothing Then
                    Me.TextBox1.Text &= "IPAddress: " + queryObj("IPAddress") & ControlChars.NewLine
                Else
                    Dim arrIPAddress As String()
                    arrIPAddress = queryObj("IPAddress")
                    For Each arrValue As String In arrIPAddress
                        Me.TextBox1.Text &= "IPAddress: {0}" + arrValue & ControlChars.NewLine
                    Next
                End If
                If queryObj("IPSubnet") Is Nothing Then
                    Me.TextBox1.Text &= "IPSubnet: " + queryObj("IPSubnet") & ControlChars.NewLine
                Else
                    Dim arrIPSubnet As String()
                    arrIPSubnet = queryObj("IPSubnet")
                    For Each arrValue As String In arrIPSubnet
                        Me.TextBox1.Text &= "IPSubnet: {0}" + arrValue & ControlChars.NewLine
                    Next
                End If
                Me.TextBox1.Text &= "MACAddress: {0}" + queryObj("MACAddress") & ControlChars.NewLine
                Me.TextBox1.Text &= "======================================" & ControlChars.NewLine

            Next
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying forWMI data: " & err.Message)
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        End
    End Sub


    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        GenerateWMIClass("Win32_NetworkAdapter")
        For Each adp As Win32.NetworkAdapter In Win32.NetworkAdapter.GetInstances("PhysicalAdapter='True'")
            If adp.PhysicalAdapter Then
                Console.WriteLine(adp.Name)
            End If
        Next

    End Sub
End Class

here is the separate class code, which is called Win32.vb:
VB.NET:
Public Class Win32
    Public Shared Sub GenerateWMIClass(ByVal name As String)
        Dim m As New Management.ManagementClass(name)
        m.GetStronglyTypedClassCode(Management.CodeLanguage.VB, name & ".vb", "Win32")
        m.Dispose()
    End Sub
End Class

I tried adding this to the main code on several places, but it just doesn't want to go.
Am I forgetting to add a reference or something?

Thanks for the effort taken so far.
Really appriciated!
 
You sure got that wrong. The GenerateWMIClass method is something you call once to have a class file generated in output folder, for example the file Win32_NetworkAdapter.vb. When this is done you add this file to your project and can use the class in your coding. The intension is to have easy to use class objects instead of all that ManagementObjectSearcher bs.
 
Back
Top