A downloadable file for this article is available here.
The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition. But I am confident that it would work with other versions of Windows (which support .NET 1.1) as well. To work with Visual Studio 2005, the coding may need minor modifications (I didn't really test it yet). If you come across any problems, please do post them in the discussion, so that I can guide you accordingly.
If you wanted to use WMI with JavaScript (on the client side using IE), you may need to regularly check www.devarticles.com for a contribution dealing with this topic.
Some global routines used for my WMI development
I already contributed several articles on WMI with Visual Basic.NET and VB Script. I'm trying to cover almost all of the latest aspects of WMI. I strongly suggest you go through my series on "WMI Programming with Visual Basic.NET" before reading this article. The series even covers "event handling" using WMI with Visual Basic .NET in the form of a Windows service.
For flexibility in retrieving WMI information (using VB.NET), I frequently used the following simple global routines which help me to work with other areas of development very easily.
Public Sub addRow(ByRef dt As DataTable, ByVal p As String, ByVal v As String)
Dimdr As DataRow
dr = dt.NewRow
dr("Property") = p
dr("Value") = v
dt.Rows.Add(dr)
End Sub
Public Function getStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Property"))
dt.Columns.Add(New DataColumn("Value"))
Return dt
End Function
Two of my global routines are the above ones, "getStructure" and "addRow." Both are generally used if I have a "Property->value" scenario. "GetStructure" just creates a data table structure and "AddRow" simply adds a data row for every data table passed to it.
How do you list all of the network adapters available on your system? We'll assume that you also want a list of all bridges and other logical network connections as well.
Before going to the WMI code, we need to define the cache to store that information. The following are the two routines I declared in "globals.vb" to hold the cache of information.
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
The following is the WMI code to retrieve our desired information.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"rootCIMV2", _
"SELECT * FROM Win32_NetworkAdapter")
Dim dt As DataTable = globals.getNetworkAdapterStructure
For Each queryObj As ManagementObject In searcher.Get ()
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_NetworkAdapter) to give you only the most important ones. The above program would list the MACAddress, Manufacturer and AdapterType. You can further extend the above program with several other properties available in the "Win32_NetworkAdapter" class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VBScript using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
In the previous section, we only displayed preliminary information. The users may need further extended information like IP address, DHCP information, and so on from network adapters using WMI. Let us see how to get it using VB.NET:
Try
Dim searcher As New ManagementObjectSearcher( _
"rootCIMV2", _
"SELECT * FROM Win32_NetworkAdapterConfiguration")
For Each queryObj As ManagementObject In searcher.Get ()
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_NetworkAdapterConfiguration) to give you only the most important ones. The above program would list the Caption, status of DHCP, DHCP Server IP if available, DNS info, IP address of the adapter and so on. You can further extend the above program with several other properties available in the "Win32_NetworkAdapterConfiguration" class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VB Script by using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
Before going into the WMI code, we need to define the cache to store that information. The following are the two routines I declared in "globals.vb" to hold the cache of information.
Public Function getNetworkConnectionStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("ConnectionState"))
dt.Columns.Add(New DataColumn("ConnectionType"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("RemoteName"))
dt.Columns.Add(New DataColumn("RemotePath"))
Return dt
End Function
Public Sub addNetworkConnectionRow(ByRef dt As DataTable, ByVal Caption As String, ByVal ConnectionState As String, ByVal ConnectionType As String, ByVal Description As String, ByVal Name As String, ByVal RemoteName As String, ByVal RemotePath As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("ConnectionState") = ConnectionState
dr("ConnectionType") = ConnectionType
dr("Description") = Description
dr("Name") = Name
dr("RemoteName") = RemoteName
dr("RemotePath") = RemotePath
dt.Rows.Add(dr)
End Sub
The following is the WMI code, to retrieve our desired information.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"rootCIMV2", _
"SELECT * FROM Win32_NetworkConnection")
Dim dt As DataTable = globals.getNetworkConnectionStructure
For Each queryObj As ManagementObject In searcher.Get ()
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_NetworkConnection) to give you only the most important ones. The above program would list the Caption, State of Connection, Type of Connection, Remote Name, Remote Path, and so on. You can further extend the above program with several other properties available in the "Win32_NetworkConnection" class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VB Script with the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
You can further delve into the classes of WMI and design your own application to retrieve almost every important aspect of network adapters. If you extend your skills to ASP.NET, then you can develop a web application which can retrieve your network information on the fly, without your presence in the office itself! I think you can imagine a similar application at the Pocket PC level! Good luck.
If you need articles on any particular topic of WMI, do not hesitate to post in the discussion. I shall definitely help you out.
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.