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

GetBestRoute

関数
指定宛先・送信元IPv4アドレスに最適な経路を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD GetBestRoute(
    DWORD dwDestAddr,
    DWORD dwSourceAddr,   // optional
    MIB_IPFORWARDROW* pBestRoute
);

パラメーター

名前方向
dwDestAddrDWORDin
dwSourceAddrDWORDinoptional
pBestRouteMIB_IPFORWARDROW*out

戻り値の型: DWORD

各言語での呼び出し定義

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

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

GetBestRoute = ctypes.windll.iphlpapi.GetBestRoute
GetBestRoute.restype = wintypes.DWORD
GetBestRoute.argtypes = [
    wintypes.DWORD,  # dwDestAddr : DWORD
    wintypes.DWORD,  # dwSourceAddr : DWORD optional
    ctypes.c_void_p,  # pBestRoute : MIB_IPFORWARDROW* out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetBestRoute = iphlpapi.NewProc("GetBestRoute")
)

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