2

I'm trying to interact with WMI through a vb.net program in order to make any machine that runs this program pull the IP settings and DNS server settings from DHCP for all network adapters with an IP.

The code I presently have works for DHCP without issue, but does not change DNS settings. The program compiles and executes without issue, but the DNS settings are not changing to be fetched automatically from DHCP.

    Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If (Not CBool(objMO("IPEnabled"))) Then
            Continue For
        End If

        Try
            Dim objNewIP As ManagementBaseObject = Nothing
            Dim objSetIP As ManagementBaseObject = Nothing
            Dim objNewDNS As ManagementBaseObject = Nothing
            Dim objSetDNS As ManagementBaseObject = Nothing

            objNewIP = objMO.GetMethodParameters("EnableDHCP")
            objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
            objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
            objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)

        Catch ex As Exception
            MessageBox.Show("Settings unchanged : " & ex.Message)
        End Try
    Next objMO

I'm so close to getting this solved, I just need help to figure out this last step.

user4404556
  • 23
  • 1
  • 4

1 Answers1

1

You annoyingly have to do it through the registry, they didn't add WMI methods for it. Specifically (taken from here: https://gallery.technet.microsoft.com/7b1cec46-bdb8-4afc-b240-9789eefce6de) you need to set this key to null.

"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" 

Below is your code with the necessary new sub inserted in

    Const conKeyPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"

    Public Sub Test()

    Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If (Not CBool(objMO("IPEnabled"))) Then
            Continue For
        End If

        Try
            Dim objNewIP As ManagementBaseObject = Nothing
            Dim objSetIP As ManagementBaseObject = Nothing
            Dim objNewDNS As ManagementBaseObject = Nothing
            Dim objSetDNS As ManagementBaseObject = Nothing

            objNewIP = objMO.GetMethodParameters("EnableDHCP")
            objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
            objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
            objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)
            SetDNSAutomatically(objMO.GetPropertyValue("settingID"))

        Catch ex As Exception
            MessageBox.Show("Settings unchanged : " & ex.Message)
        End Try
    Next objMO
End Sub
Private Sub SetDNSAutomatically(ByVal settingID As String)
    If settingID = String.Empty Then
        Throw New ArgumentNullException("settingID")
    End If

     Dim _adapterKeyPath = String.Format("{0}\{1}", conKeyPath, settingID)
    My.Computer.Registry.SetValue(_adapterKeyPath, "NameServer", String.Empty)

End Sub
Bill Dinger
  • 179
  • 1
  • 6
  • Queries such as [this](http://stackoverflow.com/questions/209779/how-can-you-change-network-settings-ip-address-dns-wins-host-name-with-code) indicate that it's possible to change DNS settings in c#, so it should be feasible in VB.NET - I just can't seem to get it sorted. – user4404556 Jan 05 '15 at 07:01
  • Ideally. Those who may wind up maintaining the code after me can't seem to grasp anything beyond basic VB.NET. The fewer loops they are thrown through, the better. – user4404556 Jan 05 '15 at 07:11
  • Seems to be throwing an exception that the registry key name must start with a valid base key name. – user4404556 Jan 06 '15 at 07:25
  • Constant value should be spelled out, it is fixed now in code above (HKEY_LOCAL_MACHINE instead of HKLM) – Bill Dinger Jan 06 '15 at 07:39
  • That took care of it! The only issue I have now is getting the loop to execute on all of the network adapters with mac addresses, not just those with IPEnabled. I attempted to swap MACAddress in for IPEnabled, but unfortunately VS returned an issue with converting a mac address xx:xx:xx:xx:xx:xx from String into Boolean was not valid. – user4404556 Jan 06 '15 at 08:03
  • @user4404556 look into using a WMI query to filter the results you get based on just IPENabled=True, or open up a new question but that's what you'd want to do ;) – Bill Dinger Jan 07 '15 at 01:42