Win32 API 日本語リファレンス
ホームNetworkManagement.IpHelper › GetBestInterfaceEx

GetBestInterfaceEx

関数
指定宛先ソケットアドレスへの最適なインターフェイス索引を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// IPHLPAPI.dll
#include <windows.h>

DWORD GetBestInterfaceEx(
    SOCKADDR* pDestAddr,
    DWORD* pdwBestIfIndex
);

パラメーター

名前方向
pDestAddrSOCKADDR*in
pdwBestIfIndexDWORD*out

戻り値の型: DWORD

各言語での呼び出し定義

// IPHLPAPI.dll
#include <windows.h>

DWORD GetBestInterfaceEx(
    SOCKADDR* pDestAddr,
    DWORD* pdwBestIfIndex
);
[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetBestInterfaceEx(
    IntPtr pDestAddr,   // SOCKADDR*
    out uint pdwBestIfIndex   // DWORD* out
);
<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetBestInterfaceEx(
    pDestAddr As IntPtr,   ' SOCKADDR*
    <Out> ByRef pdwBestIfIndex As UInteger   ' DWORD* out
) As UInteger
End Function
' pDestAddr : SOCKADDR*
' pdwBestIfIndex : DWORD* out
Declare PtrSafe Function GetBestInterfaceEx Lib "iphlpapi" ( _
    ByVal pDestAddr As LongPtr, _
    ByRef pdwBestIfIndex As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetBestInterfaceEx = ctypes.windll.iphlpapi.GetBestInterfaceEx
GetBestInterfaceEx.restype = wintypes.DWORD
GetBestInterfaceEx.argtypes = [
    ctypes.c_void_p,  # pDestAddr : SOCKADDR*
    ctypes.POINTER(wintypes.DWORD),  # pdwBestIfIndex : DWORD* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('IPHLPAPI.dll')
GetBestInterfaceEx = Fiddle::Function.new(
  lib['GetBestInterfaceEx'],
  [
    Fiddle::TYPE_VOIDP,  # pDestAddr : SOCKADDR*
    Fiddle::TYPE_VOIDP,  # pdwBestIfIndex : DWORD* out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "iphlpapi")]
extern "system" {
    fn GetBestInterfaceEx(
        pDestAddr: *mut SOCKADDR,  // SOCKADDR*
        pdwBestIfIndex: *mut u32  // DWORD* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("IPHLPAPI.dll")]
public static extern uint GetBestInterfaceEx(IntPtr pDestAddr, out uint pdwBestIfIndex);
"@
$api = Add-Type -MemberDefinition $sig -Name 'IPHLPAPI_GetBestInterfaceEx' -Namespace Win32 -PassThru
# $api::GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
#uselib "IPHLPAPI.dll"
#func global GetBestInterfaceEx "GetBestInterfaceEx" sptr, sptr
; GetBestInterfaceEx varptr(pDestAddr), varptr(pdwBestIfIndex)   ; 戻り値は stat
; pDestAddr : SOCKADDR* -> "sptr"
; pdwBestIfIndex : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "IPHLPAPI.dll"
#cfunc global GetBestInterfaceEx "GetBestInterfaceEx" var, var
; res = GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
; pDestAddr : SOCKADDR* -> "var"
; pdwBestIfIndex : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetBestInterfaceEx(SOCKADDR* pDestAddr, DWORD* pdwBestIfIndex)
#uselib "IPHLPAPI.dll"
#cfunc global GetBestInterfaceEx "GetBestInterfaceEx" var, var
; res = GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
; pDestAddr : SOCKADDR* -> "var"
; pdwBestIfIndex : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetBestInterfaceEx = iphlpapi.NewProc("GetBestInterfaceEx")
)

// pDestAddr (SOCKADDR*), pdwBestIfIndex (DWORD* out)
r1, _, err := procGetBestInterfaceEx.Call(
	uintptr(pDestAddr),
	uintptr(pdwBestIfIndex),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetBestInterfaceEx(
  pDestAddr: Pointer;   // SOCKADDR*
  pdwBestIfIndex: Pointer   // DWORD* out
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'GetBestInterfaceEx';
result := DllCall("IPHLPAPI\GetBestInterfaceEx"
    , "Ptr", pDestAddr   ; SOCKADDR*
    , "Ptr", pdwBestIfIndex   ; DWORD* out
    , "UInt")   ; return: DWORD
●GetBestInterfaceEx(pDestAddr, pdwBestIfIndex) = DLL("IPHLPAPI.dll", "dword GetBestInterfaceEx(void*, void*)")
# 呼び出し: GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
# pDestAddr : SOCKADDR* -> "void*"
# pdwBestIfIndex : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。