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
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
GetIpAddrTable (iphlpapi)
 
.
Summary
TODO - a short description

C# Signature:

[DllImport("iphlpapi.dll", SetLastError=true)]
public static extern int GetIpAddrTable(IntPtr pIpAddrTable, ref int pdwSize, bool bOrder);

VB Signature:

Declare Ansi Function
GetIpAddrTable Lib "iphlpapi.dll" (ByRef ipTable As IntPtr, ByRef tableSize As IntPtr, ByVal ordered As Boolean) As UInt32

Managed C++ Signature

[DllImport(S"IPHLPAPI.DLL", EntryPoint=S"GetIpAddrTable", SetLastError=true, CharSet=CharSet::Unicode, ExactSpelling=true)]
static UInt32 GetIpAddressTable( [Out] IntPtr ipTable, [In,Out] IntPtr tableSize, bool sort);

User-Defined Types:

None.

Alternative Managed API:

  System.Net.Dns.GetHostAddresses()

Example:

  foreach (System.Net.IPAddress strIP in System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
  {
    Console.WriteLine(strIP.ToString());
  }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

IntPtr pBuf = IntPtr.Zero;
int nBufSize = 0;
// get the required buffer size            
GetIpAddrTable( IntPtr.Zero, ref nBufSize, false );
// allocate the buffer
pBuf = Marshal.AllocHGlobal( nBufSize );
int r = GetIpAddrTable( pBuf, ref nBufSize, false );
if ( r != 0 )
    throw new System.ComponentModel.Win32Exception( r );

int entrySize = Marshal.SizeOf(typeof(MIB_IPADDRROW));
int nEntries = Marshal.ReadInt32(pBuf);
int tableStartAddr = (int)pBuf + sizeof(int);
for (int i = 0; i < nEntries; i++)
{
    IntPtr pEntry = (IntPtr)(tableStartAddr + i * entrySize);
    MIB_IPADDRROW addrStruct = (MIB_IPADDRROW)Marshal.PtrToStructure(pEntry, typeof(MIB_IPADDRROW));
    string ipAddrStr = IPToString( IPAddress.NetworkToHostOrder((int)addrStruct._address) );
    string ipMaskStr = IPToString( IPAddress.NetworkToHostOrder((int)addrStruct._mask) );
}
if ( pBuf != IntPtr.Zero )
    Marshal.FreeHGlobal( pBuf );

// helper function IPToString
static string IPToString(int ipaddr)
{
    return String.Format( "{0}.{1}.{2}.{3}",
    (ipaddr >> 24) & 0xFF, (ipaddr >> 16) & 0xFF,
    (ipaddr >> 8) & 0xFF, ipaddr & 0xFF);
}

Documentation

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
Edit This Page
Find References
Show Printable Version
Revisions