Archive

Posts Tagged ‘NIC’

Roundup – Modify NIC settings

by .hac on January 31st, 2009

I was working on “hacking” NIC settings last week, because my programs require a specific DNS server for their queries. My project is written in C#, and the PowerShell is tool that we use very often in the project (both appear in product code and test code). So there are two ways that I can do to hack the Network Interface Card (NIC) settings, use C# or PowerShell script to programmatically change the DNS server setting.

After google it, I found that Windows exposed a set of Windows Management Instrumentation classes which allows developers to access the Windows settings through VB or C++. The class that suit for my task is Win32_NetworkAdapterConfiguration. You can get this object using a wrapper in C# or directly get it with cmdlet in PowerShell. In an object of Win32_NetworkAdapterConfiguration, there is a property DnsServerSearchOrder, which is the DNS setting for the corresponding NIC. If DHCP is enabled and DnsServerSearchOrder is empty (DnsServerSearchOrder is empty implies that DHCP is enable, but not vice versa), the NIC setting is configured to get the DNS servers by DHCP, otherwise, the NIC will use what the user has specified.

In C#, we need to use a ManagementClass to wrap the Win32 classes so that we can access it through C# code. By using the object of this class, we can grab all objects of a Win32 class and have the controls of them through the ManagementObject class. After looking at the function that supported by this class, we will be able to modify the NIC settings. Here is a small piece of code containing all major functions.

private void SetDnsSetting(List ipAddresses) {
  const string SetDnsServerSearchOrder = "SetDNSServerSearchOrder";
  const string DnsServerSearchOrder = "DNSServerSearchOrder";

  ManagementClass managedClass =
    new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection managedObjectCollection =
    managedClass.GetInstances();
  foreach (ManagementObject managedObject in managedObjectCollection)
  {
    ManagementBasedObject managedBasedObject =
      managedObject.GetMethodParameters(SetDnsServerSearchOrder);
    managedBasedObject[DnsServerSearchOrder] =
      ipAddresses.ToArray();
    managedObject.InvokeMethod(
        SetDnsServerSearchOrder, managedBasedObject, null);
  }
}

We then look at PowerShell, the script looks more simple, because there is a task Get-WMIObject in PowerShell that can be used to fetch all objects of a specific WMI class. After that, we can perform all functions that supported in the WMI class. Let’s see how to modify the settings with PowerShell script.

$DnsServerSearchOrder = "127.0.0.1", "127.0.0.2"

$networksInterfaces =
  Get-WmiObject "Win32_NetworkAdapterConfiguration"
  | where {$_.IPEnabled -eq "TRUE"}

Foreach ($networkInterface in $networkInterfaces) {
  $networkInterface.SetDNSServerSearchOrder($DnsServerSearchOrder)
}

Is this more simple than implementing in C#? Yes, you now experience the powerful of scripts. But if you don’t even know what PowerShell is, I would strongly recommend you to take a look on it. There are many good resources on the web.

Two more things I want to mentioned before finishing this article. If you want to enable automatically discover DNS server, you can assign empty string or null to the SetDNSServerSearchOrder in C# or PowerShell. Besides, both implementation requires to be run by administrator!

Enjoy! Have a nice weekend.

Reference:

DNS, Windows , , , ,