Tuesday, February 07, 2006

Pain with DHCP

having searched online for all possible ways to know if the DHCP is enabled on a system, i could finally get hold some WMI way to do so. Thanks to aspfree, i did not go through it yet but woud do so very soon. Anyway heres the code:

Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Dim dt As DataTable = globals.getNetworkInfoStructure
For Each queryObj As ManagementObject In searcher.Get()
Dim IPAddress As String = ""
If queryObj("IPAddress") Is Nothing Then
IPAddress = queryObj("IPAddress")
Else
Dim arrIPAddress As String()
arrIPAddress = queryObj("IPAddress")
For Each arrValue As String In arrIPAddress
IPAddress &= arrValue
Next
End If
globals.addNetworkInfo(dt, queryObj("Caption"), queryObj("Description"), queryObj("DHCPEnabled"), queryObj("DHCPServer"), queryObj("DNSDomain"), queryObj("DNSHostName"), IPAddress)

Next
Me.DataGrid1.DataSource = dt

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

This is ofcourse only a part of the code and i shall update it very soon, once i modify it as per my need.
Anyway heres another new thing i learnt in .NET SDK.
You have this mgmtclassgen tool which you should run from visual studio commmadn prompt and generate a vb or C# or any other .net class which functions exactly like the WMI object, just that you need not use any managementobjectsearcher. More information on it later. Meanwhile just google out for "mgmtclassgen" and you ll get the information needed.
Thats it for now.

No comments: