Digging into Network Adapters with Visual Basic.NET and VBScript using WMI - Listing network connection information using WMI
(Page 4 of 4 )
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
()
globals.addNetworkConnectionRow(dt, queryObj
("Caption"), queryObj("ConnectionState"), queryObj
("ConnectionType"), queryObj("Description"), queryObj("Name"),
queryObj("RemoteName"), queryObj("RemotePath"))
Next
Me.DataGrid1.DataSource = dt
Catch err As ManagementException
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")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkConnection",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "ConnectionState: " & objItem.ConnectionState
Wscript.Echo "ConnectionType: " & objItem.ConnectionType
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "RemoteName: " & objItem.RemoteName
Wscript.Echo "RemotePath: " & objItem.RemotePath
Next
Summary
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |