Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetAdaptersInfo (iphlpapi)
 
.

C# Signature:

[DllImport("iphlpapi.dll", CharSet=CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);

VB Signature:

<DllImport("iphlpapi.dll", EntryPoint:="GetAdaptersInfo", CharSet:=CharSet.Ansi)> _
Private Shared Function GetAdaptersInfo( _
     ByVal pAdapterInfo As IntPtr, _
     ByRef pBufOutLen As UInt64) As Int32
End Function

Sample Code:

Public Sub GetAdapters()
     Dim structSize As Int32 = Marshal.SizeOf(GetType(IP_ADAPTER_INFO))
     Dim pArray As IntPtr = Marshal.AllocHGlobal(structSize)

     Dim len As UInt64 = Convert.ToUInt64(structSize)
     Dim ret As Int32 = GetAdaptersInfo(pArray, len)

     If ret = 111 Then
     ' Buffer was too small, reallocate the correct size for the buffer.
     pArray = Marshal.ReAllocHGlobal(pArray, New IntPtr(Convert.ToInt64(len)))
     ret = GetAdaptersInfo(pArray, len)
     End If

     If ret = 0 Then
     ' Call succeeded
     Dim pEntry As IntPtr = pArray
     Do
        ' Retrieve the adapter info from the memory address.
         Dim Entry As IP_ADAPTER_INFO = CType(Marshal.PtrToStructure(pEntry, GetType(IP_ADAPTER_INFO)), IP_ADAPTER_INFO)
         pEntry = Entry.Next
     Loop Until IntPtr.op_Equality(pEntry, IntPtr.Zero)
     End If

     Marshal.FreeHGlobal(pArray)
End Sub

C# Sample Code:

public static void GetAdapters()
{
   long structSize = Marshal.SizeOf( typeof( IP_ADAPTER_INFO ) );
   IntPtr pArray = Marshal.AllocHGlobal( new IntPtr(structSize) );

   int ret = GetAdaptersInfo( pArray, ref structSize );

   if (ret == ERROR_BUFFER_OVERFLOW ) // ERROR_BUFFER_OVERFLOW == 111
   {
     // Buffer was too small, reallocate the correct size for the buffer.
     pArray = Marshal.ReAllocHGlobal( pArray, new IntPtr (structSize) );

     ret = GetAdaptersInfo( pArray, ref structSize );
   } // if

   if ( ret == 0 )
   {
     // Call Succeeded
     IntPtr pEntry = pArray;

     do
     {
       // Retrieve the adapter info from the memory address
       IP_ADAPTER_INFO entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure( pEntry, typeof( IP_ADAPTER_INFO ));

       // ***Do something with the data HERE!***
       Console.WriteLine("\n");
       Console.WriteLine( "Index: {0}", entry.Index.ToString() );

       // Adapter Type
       string tmpString = string.Empty;
       switch( entry.Type )
       {
         case MIB_IF_TYPE_ETHERNET  : tmpString = "Ethernet";  break;
         case MIB_IF_TYPE_TOKENRING : tmpString = "Token Ring"; break;
         case MIB_IF_TYPE_FDDI      : tmpString = "FDDI"; break;
         case MIB_IF_TYPE_PPP       : tmpString = "PPP"; break;
         case MIB_IF_TYPE_LOOPBACK  : tmpString = "Loopback"; break;
         case MIB_IF_TYPE_SLIP      : tmpString = "Slip"; break;
         default                    : tmpString = "Other/Unknown"; break;
       } // switch
       Console.WriteLine( "Adapter Type: {0}", tmpString );

       Console.WriteLine( "Name: {0}", entry.AdapterName );
       Console.WriteLine( "Desc: {0}\n", entry.AdapterDescription );

       Console.WriteLine( "DHCP Enabled: {0}", ( entry.DhcpEnabled == 1 ) ? "Yes" : "No" );

       if (entry.DhcpEnabled == 1)
       {
         Console.WriteLine( "DHCP Server : {0}", entry.DhcpServer.IpAddress.Address );

         // Lease Obtained (convert from "time_t" to C# DateTime)
         DateTime pdatDate = new DateTime(1970, 1, 1).AddSeconds( entry.LeaseObtained ).ToLocalTime();
         Console.WriteLine( "Lease Obtained: {0}", pdatDate.ToString() );

         // Lease Expires (convert from "time_t" to C# DateTime)
         pdatDate = new DateTime(1970, 1, 1).AddSeconds( entry.LeaseExpires ).ToLocalTime();
         Console.WriteLine( "Lease Expires : {0}\n", pdatDate.ToString() );
       } // if DhcpEnabled

       Console.WriteLine( "IP Address     : {0}", entry.IpAddressList.IpAddress.Address );
       Console.WriteLine( "Subnet Mask    : {0}", entry.IpAddressList.Mask.Address );
       Console.WriteLine( "Default Gateway: {0}", entry.GatewayList.IpAddress.Address );

       // MAC Address (data is in a byte[])
       tmpString = string.Empty;
       for (int i = 0; i < entry.AddressLength - 1; i++)
       {
         tmpString += string.Format("{0:X2}-", entry.Address[i]);
       }
       Console.WriteLine( "MAC Address    : {0}{1:X2}\n", tmpString, entry.Address[entry.AddressLength - 1] );

       Console.WriteLine( "Has WINS: {0}", entry.HaveWins ? "Yes" : "No" );
       if (entry.HaveWins)
       {
         Console.WriteLine( "Primary WINS Server  : {0}", entry.PrimaryWinsServer.IpAddress.Address );
         Console.WriteLine( "Secondary WINS Server: {0}", entry.SecondaryWinsServer.IpAddress.Address );
       } // HaveWins

       // Get next adapter (if any)
       pEntry = entry.Next;

     }
     while( pEntry != IntPtr.Zero );

     Marshal.FreeHGlobal(pArray);

   } // if
   else
   {
     Marshal.FreeHGlobal(pArray);
     throw new InvalidOperationException( "GetAdaptersInfo failed: " + ret );
   }

} // GetAdapters

Notes:

GetAdaptersInfo returns a pointer to an IP_ADAPTER_INFO linked list.

Tip

In .NET 2.0, this library (The IP Helper API) is encapsulated in the NetworkInformation namespace.

Does this requiere a new dll?

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions