GetBestInterface function (iphlpapi.h)

The GetBestInterface function retrieves the index of the interface that has the best route to the specified IPv4 address.

Syntax

IPHLPAPI_DLL_LINKAGE DWORD GetBestInterface(
  [in]  IPAddr dwDestAddr,
  [out] PDWORD pdwBestIfIndex
);

Parameters

[in] dwDestAddr

The destination IPv4 address for which to retrieve the interface that has the best route, in the form of an IPAddr structure.

[out] pdwBestIfIndex

A pointer to a DWORD variable that receives the index of the interface that has the best route to the IPv4 address specified by dwDestAddr.

Return value

If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Return code Description
ERROR_CAN_NOT_COMPLETE
The operation could not be completed.
ERROR_INVALID_PARAMETER
An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the pdwBestIfIndex parameter or if the pdwBestIfIndex points to memory that cannot be written.
ERROR_NOT_SUPPORTED
The request is not supported. This error is returned if no IPv4 stack is on the local computer.
Other
Use the FormatMessage function to obtain the message string for the returned error.

Remarks

The GetBestInterface function only works with IPv4 addresses. For use with IPv6 addresses, the GetBestInterfaceEx must be used.

For information about the IPAddr data type, see Windows Data Types. To convert an IP address between dotted decimal notation and IPAddr format, use the inet_addr and inet_ntoa functions.

On Windows Vista and later, the pdwBestIfIndex parameter is treated internally by IP Helper as a pointer to a NET_IFINDEX datatype.

Requirements

Requirement Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header iphlpapi.h
Library Iphlpapi.lib
DLL Iphlpapi.dll

See also

GetBestInterfaceEx

GetBestRoute

IP Helper Function Reference

IP Helper Start Page

IPAddr

MIB_BEST_IF

Windows Data Types