Monday, May 14, 2007

IP Address information in WinNT family systems

Recently I came across an issue, where I had to dig into the details of how IP address information is stored in Windows. Our CTO asked me whether it is possible to create a simple application/script so that it will be easy for the user to change between different addresses with ease. At that moment, the network administrator was having a tough time, running to every machine, whenever there is a problem.

In the early stages of the research work itself
, I found that the IP address information is stored in the Windows registry. Some information from CodeProject guided me in that direction. The key HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\Networkcards in the registry holds all the information about the installed network cards. Different cards will have different subkeys under this key. You can see the Description and ServiceName parameters in this section.

Next phase is finding the actual information, including IP address, subnet mask and default gateway. Besides that, since the plan is to move to another network completely, we need both preferred and alternate DNS addresses. After a little bit of research work, I found the specific key, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces. It does have one or more subkeys (in GUID format) and when you open them, you can find all the required keys, IPAddress, SubnetMask, DefaultGateway and NameServer. IPAddress, SubnetMask and DefaultGateway keys are of type REG_MULTI_SZ and the DNS information is stored in a single REG_SZ value, separated by commas, under the name NameServer.

Now we have the required information. It must be easy to create a simple program for the management of IP information. Win32 API functions RegOpenKeyEx, RegEnumKeyEx, RegQueryValueEx, RegSetValueEx etc. can be used for the manipulation of registry. If you are using a .Net family language, built-in classes are also available. Care should be taken about the fact that REG_MULTI_SZ value ends with two null-characters, instead of one null character.

After changing the values in the registry, I found yet another issue - bit serious one. It is possible to change the text value in the registry, but the system is still holding the previous value. So it was necessary to notify the change. After spending some time digging through all the articles, I found a method. It is by using the DhcpNotifyConfigChange function, which is implemented in the Dhcpcsvc.dll. It is not properly documented in MSDN. We can obtain a pointer to this function using the GetProcAddress function call and then it is possible to send the notification message.

The rest is all imagination. You can do whatever you want to make your application more user-friendly. Creating and managing profiles (storing all the required information in the ini files for easy switching of addresses), listing all the available adapters and addresses in the system etc. are some of them. We dont need any advanced controls for doing this.

Thats all about the background information. Hope that it was useful for you. Happy programming...

No comments: