Click here to Skip to main content
Licence 
First Posted 18 Aug 2003
Views 66,232
Bookmarked 40 times

How to obtain the current TCP/IP configuation for all adapters

By | 18 Aug 2003 | Article
An article on obtaining the current TCP/IP configuration for all .NET supported OSs.

Introduction

Obtaining the current TCP/IP configuration, is not provided by the standard .NET class libraries. In order to work on all the standard desktop OSs supported by .NET, at least two different approaches must be taken. For more recent OSs such as Windows XP, WMI contains objects that can be queried to obtain this information. The System.Management namespace provides access to WMI objects in managed code. For older OSs like Windows 98, an entirely different approach is needed. Since running the .NET Framework on Windows 98 also requires Internet Explorer 5.x or higher we can also assume that the library IPHLPAPI.DLL is available. This library commonly called IPHelper provides the API calls needed to obtain network configuration. Since IPHLPAPI is not managed code, a managed code wrapper needs to be created to use the .NET Interop services to invoke IPHLPAPI routines.

Using the code

The source code project provides two classes. The primary class is called NetworkConfiguration. NetworkConfiguration makes the determination internally regarding whether to use WMI or IPHLPAPI.DLL. It provides public methods such as GetTCPIPSettings() that returns an ArrayList of AdapterTCPIPSettings instances. AdapterSettings contains the basic configuration data such as the adapter description, IP addresses, subnets, and gateways. There are several other methods in NetworkConfiguration that serve as helper functions to obtain very task specific information from GetTCPIPSettings(). GetDNSServers() is one such method.

The second class is the managed code wrapper that calls into several APIs from IPHLPAPI.DLL. It is used internally by NetworkConfiguration only for OSs that do not support WMI. This class can also be used outside of NetworkConfiguration, if needed.

An example of using NetworkConfiguration is provided in the downloadable example attached to this article. The following code snippet demonstrates enumerating through the adapters and doing something with its members.

using MooseNet;
...

ArrayList settings = NetworkConfiguration.GetTCPIPSettings();
foreach ( NetworkConfiguration.AdapterTCPIPSettings setting in settings ) 
{ 
    if ( setting._description != null )
    {
        ...
    }
    ...
}

Points of interest

Something to keep in mind is that the WMI query can be rather slow. Do not call GetTCPIPSettings() very often in your code. The helper methods in NetworkConfiguration use a caching mechanism to avoid this. Another thing is that the AdapterSettings instances will frequently contain null references for data that can not be obtained. This should be checked before using any of its values.

You will also notice that these classes are part of a namespace called MooseNet. MooseNet is my open source networking library written in C#. Please check out my project page at http://www.hypermoose.com. You will find many useful classes for handling protocols such as UPnP, SMTP, POP3 and DNS there.

History

  • Initial version - August 10, 2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gary Caldwell

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhypermoose.com PinmemberRamon Smits23:14 3 Jan '07  
QuestionPorting to PocketPC Pinmemberddellarossa23:00 8 Feb '06  
Hi,
I appreciate very much your article and I tried to leave my positive feedback, but I were not able to do, due to a server error...
Anyway, I implemented your code in a windows library and all works well. But when I tried to port it to a PocketPC library, the code compiled, but during execution I received a runtime error "NotSupportedException" at this line
 
_IP_ADAPTER_INFO adapter = (_IP_ADAPTER_INFO)Marshal.PtrToStructure(pos, typeof(_IP_ADAPTER_INFO));
 
hence I think that the call to dll "iphlpapi.dll" return something, but when marshalling, something happens.
 
Have you any idea to resolve this problem, or a little hint to understand the cause?
Many Thanks
David Della Rossa
 

 
Here is the interested portion of code
----------------------------------------------------------------------
 
[DllImport("iphlpapi.dll", EntryPoint = "GetAdaptersInfo", SetLastError = true)]
 
private static extern int InternalGetAdaptersInfo(IntPtr pIpAdapterInfo, ref uint pulSize);
 

public static int GetAdaptersInfo(ref ArrayList infos)
{
IntPtr lpBuffer = IntPtr.Zero;
uint size = 0;
 
infos = new ArrayList();
 
int res = InternalGetAdaptersInfo(lpBuffer, ref size);
if (res != General.NO_ERROR)
{
lpBuffer = Marshal.AllocHGlobal((int)size);
res = InternalGetAdaptersInfo(lpBuffer, ref size);
if (res != General.NO_ERROR)
{
Marshal.FreeHGlobal(lpBuffer);
lpBuffer = IntPtr.Zero;
return res;
}
}
 
IntPtr pos = lpBuffer;
 
while (pos != IntPtr.Zero)
{
IP_ADAPTER_INFO info = new IP_ADAPTER_INFO();
_IP_ADAPTER_INFO adapter = (_IP_ADAPTER_INFO)Marshal.PtrToStructure(pos, typeof(_IP_ADAPTER_INFO)); //###this line raises an exception###
 
info.adapterName = adapter.adapterName;
info.description = adapter.description;
info.address = new byte[adapter.addressLength];
//etc...
 
-----------------------------------------------------------------------------
 

 
-- modified at 5:01 Thursday 9th February, 2006
GeneralSetTcpEntry Function PinmemberVitoto4:39 16 May '05  
QuestionWMI? Pinmemberbouli0:57 26 Oct '04  
AnswerRe: WMI? PinmemberVitoto5:38 19 May '05  
GeneralRemote adapter info Pinmemberserberwww21:53 13 Sep '04  
GeneralRe: Remote adapter info PinmemberPrasadGVL23:59 10 Oct '04  
GeneralWill this work for Non-Admin Logins Pinmembertanveerakl20:49 22 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 19 Aug 2003
Article Copyright 2003 by Gary Caldwell
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid