The "New ipconfig" and the IP Helper API

This article details a class that allows the manipulation of network adapters and uses that class to build a graphical ipconfig utility for anyone using Windows2000 or Windows XP. In order to accomplish this, the IP helper API is used.

Introduction

The main class presented here actually came about secondary to my desire to have a GUI replacement for the console ipconfig utility. Awhile back I was doing a lot of network related stuff and got tired of continually bringing up the command prompt which I would always close again without fail. After a few different versions the one I am actually happy with was born. Behold NetCfg-The Sequel.

Can be minimized to the system tray where a tooltip is display when hovering that contains IP data.

The Network Adapter Class

First a quick note: In my personal library this class exists as an MFC/ATL based class by which I mean it uses many classes in the new VS.NET are supported in non MFC projects but would not be supported in VS6 such as CTime and CString. I also used other constructs for the arrays such as CAtlArray rather than all of the STL based classes that are in this project. I used the STL here to minimize portability issues for those wishing to use this class in an application developed in the VS6 environment. Anyone wanting the MFC version ( for some reason ) may feel free to contact me about it.

Aside from the class interface itself there is one other thing that must be mentioned in order for this class to be used effectively and that is the function used to enumerate the adapters on the computer. This function looks like:

This function works like many of the standard windows functions that enumerate items. You must pass it a buffer and the total size of that buffer in bytes along with the parameter used to return to you the actual amount of bytes needed. If your buffer is too small ERROR_INSUFFICIENT_BUFFER is returned.

Operations

Enumeration

To do anything with this class the first thing you need to do is actually set one up. While you can feel free to do the enumeration yourself and call the setup function I have provided an enumeration function that will make your life a bit easier should you wish it. This is where much of the actual work happens.

In order to get the adapter information the first thing we do is call into the IP function GetAdaptersInfo. Once we have that list we are going to have to make sure if we got enough space from the caller to store the new list of adapters in. If we don't have a large enough buffer we cleanup the memory we allocated and return the error back to the caller along with the total amount of space that is needed to complete the operation so that the user can resize his buffer and perform the call again. If the buffer we received was large enough than we can now iterate through the entire list of adapters setting up each adapter in sequence. To do this the member function SetupAdapterInfo is called.

While just a bit lengthy this function is very simple in nature. It simply transfers much of the data from the IP_ADAPTER_INFO structure to our member variables so they can be used and retrieved later. This is also where we ensure that we account for the possibility of having more than one IP address for local IP address and Gateways. (multiple IPs and Gateways are supported in this class but are not used in the demo application.) The next thing we do here is collect DNS information for the adapter which is given to us in the IP_PER_ADAPTER_INFO structure. To get this information we must call the ::GetPerAdapterInfo API function. It doesn't make a lot of sense to me why the DNS information was not kept in the adapter structure like everything else but none the less it isn't. Finally, we just need to cleanup our allocated memory and exit the function.

Renew & Release

While there are separate functions in the interface used to release or renew an adapter this is really only one function that we use. We can do this because the operations required to renew or release an address are the exact same except for the actual API call to either renew or release. To solve this we use a single function that is passed a pointer to the proper API function for the renew/release operations.

The first that that we do when we get here is to enumerate the system's adapters and iterate the list that we get back until we find the entry that identify the same adapter that we represent. Once we have this we can that call whatever IP function was passed to us with that data and the operation will be performed returning to us the error code. All that is left to do is deallocate our memory and return back.

Note about deallocation: Looking back I suppose it would have been safer to put the deallocate block outside of the loop so that it is guaranteed to be deleted however, the adapter is always going to be found because we our enumerating hardware. The only way the adapter itself could not be found during this loop is if the application was started, the adapter was removed and then someone tried to release the adapter. That would require hotswapable cards (since the hardware would be running) which means this is running on a high end server which was not my target audience.

Misc

There are a lot of other functions here that you can feel free to review on your own. They are very simple and really don't warrant any real attention here as they are mainly accessor function for data retrieved from the adapter and per adapter info structures.

Finishing Up

Well, I hope some people learn something useful from this code and for those who don't I at least hope you now have access to a nice little utility that will replace the command line version. The one sad note about this utility is that is doesn't support NT4 because of the iphlpapi.lib requirement. This hasn't been a real problem for me as I don't use NT4 anymore but it still would have been nice.

Demo Notes

The only real note I want to make about the demo is about machine with more that one adapter and my alleged support for it. For anyone who doesn't have more than one adapter you will not get to see how this works but trust me it is there. For those of you who have multiple NICs, there is a small spinner next to the adapter name which can be used to move through your list of adapters on the computer.

Future Improvements

The one thing on my plate right now as an improvement to this software is to take hold of the MAC address. I have made some provisions for this in the code present here but it is not done as of yet. This is mainly because this code itself is pretty old for me. I just dusted it off and presented here recently. One of those thing meant to do a long while ago but kept getting pushed off. Shame how that works. Feel free to send me suggestions/comments/concerns/requests about this application or class in general.

History

Initial Release

Credit

Davide Calabro's CXPStyleButtonST and associated classes were used in the demo application. Thanks Davide!

In Micrsoft VC++ 2005, the time_t is the __time64_t structure, allows dates to be expressed up through 23:59:59, December 31, 3000, coordinated universal time (UTC).So, the Program will be throw an error that the LeaseExpire greater than macro _MAX__TIME64_T.

For the problem, you can use the macro _USE_32BIT_TIME_T to force the VC++ 2005 treat time_t as _time32_t.

There is a potential problem in the following code block. If there is two adapter in one interface and the second adapter is the one which i want to release/renew, the code will fail. So I change the code to enumerate the interface to find the right adapter. Any comment appreciate!

Use Stephane Rodriguez' VC++7 to VC++6 project converter found at
http://www.codeproject.com/tools/prjconverter.asp.
Rename or copy the .sln file to a simple (8 char) name first.
Worked fine for me - compiled with a bunch of warnings and one error.
Change pSpin->SetPos32 to pSpin->SetPos to remove the error.

Your code is really nice and meets my expectations
except that I miss the information about the status of a network.
Sample:
- network cable is removed
- network is active
- network is disabled
Can this be done?

webhost:
how can I know that the option of "Obtain DNS server address
automatically" in Internet Protocol(TCP\IP) property have be set or checked by coding ? I need to judge it and change "Obtain DNS server address
automatically" to "Use the following DNS server addresses";
Are there any api or other methods to get it?

Can you give me a dll or atleast tell me how to do it so that i can use it with c#. This is what i tried.
# Start Visual Studio .NET.
# Go to File->New->Project.
# Select Visual C++ Project, and from the “Templates”, select “Win32 Project”.
# Select DLL from “Application Type” (“Application Settings” tab).
# Check “Empty Project”

I downloaded the project from "code project" and tried to compile it.

I get the following errors:

Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'char [16]' (or there is no acceptable conversion) c:\documents and settings\administrator\desktop\netcfg_src\netadapter.cpp 143

Hi,
i'm trymg to disable and enable programmatically the network adapter of my PC, but i can't find a way ! It seems ONLY DCHP connection can be disabled !
There is a way to disable the Network adapter when it's not configured by DCHP?
At the end i want a program equivalent to the pressing of the disable menu item in the connection window. Can You hel me?
Many Thanks in advance

Its easy to disable/enable network adaptor you like programmatically.Check out the netsh utility with the context as interface ip.The command is netsh interface ip set interface "NameofInterface" ENABLED.

However but on repitive checking its found that sometimes this command doesn't work so another way to disable/enable network adaptor is to use vb script for enable.