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

GetBestInterface

関数
指定IPv4宛先への送信に最適なインターフェイス索引を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD GetBestInterface(
    DWORD dwDestAddr,
    DWORD* pdwBestIfIndex
);

パラメーター

名前方向
dwDestAddrDWORDin
pdwBestIfIndexDWORD*out

戻り値の型: DWORD

各言語での呼び出し定義

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

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

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

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetBestInterface = iphlpapi.NewProc("GetBestInterface")
)

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