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
getbestinterface (iphlpapi)
 
.
Summary
The GetBestInterface function retrieves index of the interface that has the best route to the specified IP address.

C# Signature:

[DllImport("iphlpapi.dll", SetLastError=true)]
static extern int GetBestInterface(UInt32 DestAddr, out UInt32 BestIfIndex);

VB Signature:

Declare Function GetBestInterface Lib "iphlpapi.dll" (TODO) As TODO

User-Defined Types:

None.

Notes:

DestAddr is a 32 bit representation of the four IP bytes. For .NET 1.0, you can get this from IPAddress.Address. In .NET 1.1, this is obsolete. You can use BitConverter.ToUInt32(IPAddress.GetAddressBytes()) instead.

Tips & Tricks:

Use GetAdaptersInfo to determine which adapter the index corresponds to.

Sample Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.Runtime.InteropServices;

namespace ConsoleApplication3

{

    public class myint
    {
    [DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
    public static extern int GetBestInterface(UInt32 DestAddr, out UInt32 BestIfIndex);
    }

    class Program
    {


    static void Main(string[] args)
    {
        try
        {
        IPHostEntry hostInfo = Dns.GetHostEntry("www.yahoo.com");
        // Get the IP address list that resolves to the host names contained in the
        // Alias property.
        IPAddress[] address = hostInfo.AddressList;
        // Get the alias names of the addresses in the IP address list.
        /*String[] alias = hostInfo.Aliases;

        Console.WriteLine("Host name : " + hostInfo.HostName);
        Console.WriteLine("\nAliases : ");
        for (int index = 0; index < alias.Length; index++)
        {
            Console.WriteLine(alias[index]);
        }
        Console.WriteLine("\nIP Address list :");
        for (int index = 0; index < address.Length; index++)
        {
            Console.WriteLine(address[index]);
        }*/
        myint i = new myint();
        byte[] byteArray1 = IPAddress.Parse(address[0].ToString()).GetAddressBytes();
        byte[] byteArray2 = IPAddress.Parse(address[0].ToString()).GetAddressBytes();
        byteArray1[0] = byteArray2[3];
        byteArray1[1] = byteArray2[2];
        byteArray1[2] = byteArray2[1];
        byteArray1[3] = byteArray2[0];
        UInt32 ipaddr =  BitConverter.ToUInt32(byteArray1, 0);

        UInt32 interfaceindex =0;
        myint.GetBestInterface(ipaddr, out interfaceindex);
        }
        catch (SocketException e)
        {
        Console.WriteLine("SocketException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
        }
        catch (ArgumentNullException e)
        {
        Console.WriteLine("ArgumentNullException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
        }
        catch (NullReferenceException e)
        {
        Console.WriteLine("NullReferenceException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
        }
        catch (Exception e)
        {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
        }

    }
    }

}

Alternative Managed API:

Do you know one? Please contribute it!

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