ホーム › NetworkManagement.IpHelper › GetBestRoute2
GetBestRoute2
関数指定送信元・宛先アドレスに最適な経路と送信元を取得する。
シグネチャ
// IPHLPAPI.dll
#include <windows.h>
WIN32_ERROR GetBestRoute2(
NET_LUID_LH* InterfaceLuid, // optional
DWORD InterfaceIndex,
const SOCKADDR_INET* SourceAddress, // optional
const SOCKADDR_INET* DestinationAddress,
DWORD AddressSortOptions,
MIB_IPFORWARD_ROW2* BestRoute,
SOCKADDR_INET* BestSourceAddress
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| InterfaceLuid | NET_LUID_LH* | inoptional |
| InterfaceIndex | DWORD | in |
| SourceAddress | SOCKADDR_INET* | inoptional |
| DestinationAddress | SOCKADDR_INET* | in |
| AddressSortOptions | DWORD | in |
| BestRoute | MIB_IPFORWARD_ROW2* | out |
| BestSourceAddress | SOCKADDR_INET* | out |
戻り値の型: WIN32_ERROR
各言語での呼び出し定義
// IPHLPAPI.dll
#include <windows.h>
WIN32_ERROR GetBestRoute2(
NET_LUID_LH* InterfaceLuid, // optional
DWORD InterfaceIndex,
const SOCKADDR_INET* SourceAddress, // optional
const SOCKADDR_INET* DestinationAddress,
DWORD AddressSortOptions,
MIB_IPFORWARD_ROW2* BestRoute,
SOCKADDR_INET* BestSourceAddress
);[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetBestRoute2(
IntPtr InterfaceLuid, // NET_LUID_LH* optional
uint InterfaceIndex, // DWORD
IntPtr SourceAddress, // SOCKADDR_INET* optional
IntPtr DestinationAddress, // SOCKADDR_INET*
uint AddressSortOptions, // DWORD
IntPtr BestRoute, // MIB_IPFORWARD_ROW2* out
IntPtr BestSourceAddress // SOCKADDR_INET* out
);<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetBestRoute2(
InterfaceLuid As IntPtr, ' NET_LUID_LH* optional
InterfaceIndex As UInteger, ' DWORD
SourceAddress As IntPtr, ' SOCKADDR_INET* optional
DestinationAddress As IntPtr, ' SOCKADDR_INET*
AddressSortOptions As UInteger, ' DWORD
BestRoute As IntPtr, ' MIB_IPFORWARD_ROW2* out
BestSourceAddress As IntPtr ' SOCKADDR_INET* out
) As UInteger
End Function' InterfaceLuid : NET_LUID_LH* optional
' InterfaceIndex : DWORD
' SourceAddress : SOCKADDR_INET* optional
' DestinationAddress : SOCKADDR_INET*
' AddressSortOptions : DWORD
' BestRoute : MIB_IPFORWARD_ROW2* out
' BestSourceAddress : SOCKADDR_INET* out
Declare PtrSafe Function GetBestRoute2 Lib "iphlpapi" ( _
ByVal InterfaceLuid As LongPtr, _
ByVal InterfaceIndex As Long, _
ByVal SourceAddress As LongPtr, _
ByVal DestinationAddress As LongPtr, _
ByVal AddressSortOptions As Long, _
ByVal BestRoute As LongPtr, _
ByVal BestSourceAddress As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetBestRoute2 = ctypes.windll.iphlpapi.GetBestRoute2
GetBestRoute2.restype = wintypes.DWORD
GetBestRoute2.argtypes = [
ctypes.c_void_p, # InterfaceLuid : NET_LUID_LH* optional
wintypes.DWORD, # InterfaceIndex : DWORD
ctypes.c_void_p, # SourceAddress : SOCKADDR_INET* optional
ctypes.c_void_p, # DestinationAddress : SOCKADDR_INET*
wintypes.DWORD, # AddressSortOptions : DWORD
ctypes.c_void_p, # BestRoute : MIB_IPFORWARD_ROW2* out
ctypes.c_void_p, # BestSourceAddress : SOCKADDR_INET* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('IPHLPAPI.dll')
GetBestRoute2 = Fiddle::Function.new(
lib['GetBestRoute2'],
[
Fiddle::TYPE_VOIDP, # InterfaceLuid : NET_LUID_LH* optional
-Fiddle::TYPE_INT, # InterfaceIndex : DWORD
Fiddle::TYPE_VOIDP, # SourceAddress : SOCKADDR_INET* optional
Fiddle::TYPE_VOIDP, # DestinationAddress : SOCKADDR_INET*
-Fiddle::TYPE_INT, # AddressSortOptions : DWORD
Fiddle::TYPE_VOIDP, # BestRoute : MIB_IPFORWARD_ROW2* out
Fiddle::TYPE_VOIDP, # BestSourceAddress : SOCKADDR_INET* out
],
-Fiddle::TYPE_INT)#[link(name = "iphlpapi")]
extern "system" {
fn GetBestRoute2(
InterfaceLuid: *mut NET_LUID_LH, // NET_LUID_LH* optional
InterfaceIndex: u32, // DWORD
SourceAddress: *const SOCKADDR_INET, // SOCKADDR_INET* optional
DestinationAddress: *const SOCKADDR_INET, // SOCKADDR_INET*
AddressSortOptions: u32, // DWORD
BestRoute: *mut MIB_IPFORWARD_ROW2, // MIB_IPFORWARD_ROW2* out
BestSourceAddress: *mut SOCKADDR_INET // SOCKADDR_INET* out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("IPHLPAPI.dll")]
public static extern uint GetBestRoute2(IntPtr InterfaceLuid, uint InterfaceIndex, IntPtr SourceAddress, IntPtr DestinationAddress, uint AddressSortOptions, IntPtr BestRoute, IntPtr BestSourceAddress);
"@
$api = Add-Type -MemberDefinition $sig -Name 'IPHLPAPI_GetBestRoute2' -Namespace Win32 -PassThru
# $api::GetBestRoute2(InterfaceLuid, InterfaceIndex, SourceAddress, DestinationAddress, AddressSortOptions, BestRoute, BestSourceAddress)#uselib "IPHLPAPI.dll"
#func global GetBestRoute2 "GetBestRoute2" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; GetBestRoute2 varptr(InterfaceLuid), InterfaceIndex, varptr(SourceAddress), varptr(DestinationAddress), AddressSortOptions, varptr(BestRoute), varptr(BestSourceAddress) ; 戻り値は stat
; InterfaceLuid : NET_LUID_LH* optional -> "sptr"
; InterfaceIndex : DWORD -> "sptr"
; SourceAddress : SOCKADDR_INET* optional -> "sptr"
; DestinationAddress : SOCKADDR_INET* -> "sptr"
; AddressSortOptions : DWORD -> "sptr"
; BestRoute : MIB_IPFORWARD_ROW2* out -> "sptr"
; BestSourceAddress : SOCKADDR_INET* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "IPHLPAPI.dll" #cfunc global GetBestRoute2 "GetBestRoute2" var, int, var, var, int, var, var ; res = GetBestRoute2(InterfaceLuid, InterfaceIndex, SourceAddress, DestinationAddress, AddressSortOptions, BestRoute, BestSourceAddress) ; InterfaceLuid : NET_LUID_LH* optional -> "var" ; InterfaceIndex : DWORD -> "int" ; SourceAddress : SOCKADDR_INET* optional -> "var" ; DestinationAddress : SOCKADDR_INET* -> "var" ; AddressSortOptions : DWORD -> "int" ; BestRoute : MIB_IPFORWARD_ROW2* out -> "var" ; BestSourceAddress : SOCKADDR_INET* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "IPHLPAPI.dll" #cfunc global GetBestRoute2 "GetBestRoute2" sptr, int, sptr, sptr, int, sptr, sptr ; res = GetBestRoute2(varptr(InterfaceLuid), InterfaceIndex, varptr(SourceAddress), varptr(DestinationAddress), AddressSortOptions, varptr(BestRoute), varptr(BestSourceAddress)) ; InterfaceLuid : NET_LUID_LH* optional -> "sptr" ; InterfaceIndex : DWORD -> "int" ; SourceAddress : SOCKADDR_INET* optional -> "sptr" ; DestinationAddress : SOCKADDR_INET* -> "sptr" ; AddressSortOptions : DWORD -> "int" ; BestRoute : MIB_IPFORWARD_ROW2* out -> "sptr" ; BestSourceAddress : SOCKADDR_INET* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; WIN32_ERROR GetBestRoute2(NET_LUID_LH* InterfaceLuid, DWORD InterfaceIndex, SOCKADDR_INET* SourceAddress, SOCKADDR_INET* DestinationAddress, DWORD AddressSortOptions, MIB_IPFORWARD_ROW2* BestRoute, SOCKADDR_INET* BestSourceAddress) #uselib "IPHLPAPI.dll" #cfunc global GetBestRoute2 "GetBestRoute2" var, int, var, var, int, var, var ; res = GetBestRoute2(InterfaceLuid, InterfaceIndex, SourceAddress, DestinationAddress, AddressSortOptions, BestRoute, BestSourceAddress) ; InterfaceLuid : NET_LUID_LH* optional -> "var" ; InterfaceIndex : DWORD -> "int" ; SourceAddress : SOCKADDR_INET* optional -> "var" ; DestinationAddress : SOCKADDR_INET* -> "var" ; AddressSortOptions : DWORD -> "int" ; BestRoute : MIB_IPFORWARD_ROW2* out -> "var" ; BestSourceAddress : SOCKADDR_INET* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR GetBestRoute2(NET_LUID_LH* InterfaceLuid, DWORD InterfaceIndex, SOCKADDR_INET* SourceAddress, SOCKADDR_INET* DestinationAddress, DWORD AddressSortOptions, MIB_IPFORWARD_ROW2* BestRoute, SOCKADDR_INET* BestSourceAddress) #uselib "IPHLPAPI.dll" #cfunc global GetBestRoute2 "GetBestRoute2" intptr, int, intptr, intptr, int, intptr, intptr ; res = GetBestRoute2(varptr(InterfaceLuid), InterfaceIndex, varptr(SourceAddress), varptr(DestinationAddress), AddressSortOptions, varptr(BestRoute), varptr(BestSourceAddress)) ; InterfaceLuid : NET_LUID_LH* optional -> "intptr" ; InterfaceIndex : DWORD -> "int" ; SourceAddress : SOCKADDR_INET* optional -> "intptr" ; DestinationAddress : SOCKADDR_INET* -> "intptr" ; AddressSortOptions : DWORD -> "int" ; BestRoute : MIB_IPFORWARD_ROW2* out -> "intptr" ; BestSourceAddress : SOCKADDR_INET* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
procGetBestRoute2 = iphlpapi.NewProc("GetBestRoute2")
)
// InterfaceLuid (NET_LUID_LH* optional), InterfaceIndex (DWORD), SourceAddress (SOCKADDR_INET* optional), DestinationAddress (SOCKADDR_INET*), AddressSortOptions (DWORD), BestRoute (MIB_IPFORWARD_ROW2* out), BestSourceAddress (SOCKADDR_INET* out)
r1, _, err := procGetBestRoute2.Call(
uintptr(InterfaceLuid),
uintptr(InterfaceIndex),
uintptr(SourceAddress),
uintptr(DestinationAddress),
uintptr(AddressSortOptions),
uintptr(BestRoute),
uintptr(BestSourceAddress),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction GetBestRoute2(
InterfaceLuid: Pointer; // NET_LUID_LH* optional
InterfaceIndex: DWORD; // DWORD
SourceAddress: Pointer; // SOCKADDR_INET* optional
DestinationAddress: Pointer; // SOCKADDR_INET*
AddressSortOptions: DWORD; // DWORD
BestRoute: Pointer; // MIB_IPFORWARD_ROW2* out
BestSourceAddress: Pointer // SOCKADDR_INET* out
): DWORD; stdcall;
external 'IPHLPAPI.dll' name 'GetBestRoute2';result := DllCall("IPHLPAPI\GetBestRoute2"
, "Ptr", InterfaceLuid ; NET_LUID_LH* optional
, "UInt", InterfaceIndex ; DWORD
, "Ptr", SourceAddress ; SOCKADDR_INET* optional
, "Ptr", DestinationAddress ; SOCKADDR_INET*
, "UInt", AddressSortOptions ; DWORD
, "Ptr", BestRoute ; MIB_IPFORWARD_ROW2* out
, "Ptr", BestSourceAddress ; SOCKADDR_INET* out
, "UInt") ; return: WIN32_ERROR●GetBestRoute2(InterfaceLuid, InterfaceIndex, SourceAddress, DestinationAddress, AddressSortOptions, BestRoute, BestSourceAddress) = DLL("IPHLPAPI.dll", "dword GetBestRoute2(void*, dword, void*, void*, dword, void*, void*)")
# 呼び出し: GetBestRoute2(InterfaceLuid, InterfaceIndex, SourceAddress, DestinationAddress, AddressSortOptions, BestRoute, BestSourceAddress)
# InterfaceLuid : NET_LUID_LH* optional -> "void*"
# InterfaceIndex : DWORD -> "dword"
# SourceAddress : SOCKADDR_INET* optional -> "void*"
# DestinationAddress : SOCKADDR_INET* -> "void*"
# AddressSortOptions : DWORD -> "dword"
# BestRoute : MIB_IPFORWARD_ROW2* out -> "void*"
# BestSourceAddress : SOCKADDR_INET* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。