Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:33:30+00:00 2026-05-25T22:33:30+00:00

I know the index of network interface returned by WinAPI’s GetBestInterface . How do

  • 0

I know the index of network interface returned by WinAPI’s GetBestInterface. How do I get interface properties (IPv4 address) based on interface’s index?

Here is the working C++ code, but I need it in C#.

PMIB_IPADDRTABLE    pAddrTable;
PMIB_IPADDRROW      pAddrRow;
in_addr             ia;


CBasePage::OnSetActive();

m_edit1.SetFont(&m_font);
m_edit1.SetWindowText("");

GetIpAddrTable((PMIB_IPADDRTABLE) m_pBuffer, &m_ulSize, TRUE);

m_pBuffer = new BYTE[m_ulSize];
if (NULL != m_pBuffer)
{
    m_dwResult = GetIpAddrTable((PMIB_IPADDRTABLE) m_pBuffer, &m_ulSize, TRUE);
    if (m_dwResult == NO_ERROR)
    {
        pAddrTable = (PMIB_IPADDRTABLE) m_pBuffer;

        for (int x = 0; x < pAddrTable->dwNumEntries; x++)
        {
            pAddrRow = (PMIB_IPADDRROW) &(pAddrTable->table[x]);

            ia.S_un.S_addr = pAddrRow->dwAddr;
            m_strText.Format("       IP address: %s\r\n", inet_ntoa(ia));
            m_edit1.ReplaceSel(m_strText);

            m_strText.Format("  Interface index: %lu\r\n", pAddrRow->dwIndex);
            m_edit1.ReplaceSel(m_strText);

            ia.S_un.S_addr = pAddrRow->dwMask;
            m_strText.Format("      Subnet mask: %s\r\n", inet_ntoa(ia));
            m_edit1.ReplaceSel(m_strText);

            ia.S_un.S_addr = pAddrRow->dwBCastAddr;
            m_strText.Format("Broadcast address: %s\r\n", inet_ntoa(ia));
            m_edit1.ReplaceSel(m_strText);

            m_edit1.ReplaceSel("\r\n");
        }
    }
    else
    {
        m_strText.Format("GetIpAddrTable() failed.  Result = %lu\r\n", m_dwResult);
        m_edit1.ReplaceSel(m_strText);
    }

    delete [] m_pBuffer;
}

I’ve tried the example on pinvoke but it returns 0.0.0.0 for all the interfaces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:33:31+00:00Added an answer on May 25, 2026 at 10:33 pm

    It works for me:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Net;
    
    namespace IpInfo
    {
        [StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
        struct MIB_IPADDRROW 
        {
            public int         _address;
            public int         _index;
            public int         _mask;
            public int         _broadcastAddress;
            public int         _reassemblySize;
            public ushort    _unused1;
            public ushort    _type;
        }
    
        class Program
        {
            [DllImport("iphlpapi.dll", SetLastError=true)]
            public static extern int GetIpAddrTable(IntPtr pIpAddrTable, ref int pdwSize, bool bOrder);
    
            static void Main(string[] args)
            {
                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 );
    
                try
                {
                    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 &lt nEntries; i++)
                    {
                        IntPtr pEntry = (IntPtr)(tableStartAddr + i * entrySize);
                        MIB_IPADDRROW addrStruct = (MIB_IPADDRROW)Marshal.PtrToStructure(pEntry, typeof(MIB_IPADDRROW));
                        string ipAddrStr = IPToString(IPAddress.NetworkToHostOrder(addrStruct._address));
                        string ipMaskStr = IPToString(IPAddress.NetworkToHostOrder(addrStruct._mask));
                        Console.WriteLine("IP:" + ipAddrStr + " Mask:" + ipMaskStr);
                    }
                }
                finally
                {
                    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);
            }
        }
    }
    
    

    Produces output like this on my machine:

    IP:127.0.0.1 Mask:255.0.0.0
    IP:192.168.1.3 Mask:255.255.255.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a working fix for the ie z-index bug? I have
Here is my list list=[1,2,1,1,1,1,1,4,5] Now i want to know the index of 5th
I want to know column index of header in AdvancedDataGrid when a user clicks
After googling i came to know that Index seek is better than scan. How
I'm trying to find out a way to know the index of a anchor
I have a xml document. I know the index where i need to insert
Ok, I'm not great in mysql, but I know an index would help me
Is there a progmmatic way in Lucene to know if the index is optimized
I know how to use INDEX as in the following code. And I know
I know that a SQL Server full text index can not index more than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.