ホーム › NetworkManagement.IpHelper › GetBestInterface
GetBestInterface
関数指定IPv4宛先への送信に最適なインターフェイス索引を取得する。
シグネチャ
// IPHLPAPI.dll
#include <windows.h>
DWORD GetBestInterface(
DWORD dwDestAddr,
DWORD* pdwBestIfIndex
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| dwDestAddr | DWORD | in | 最適なルートを持つインターフェイスを取得する対象となる、宛先の IPv4 アドレス。IPAddr 構造体の形式で指定します。 |
| pdwBestIfIndex | DWORD* | out | dwDestAddr で指定した IPv4 アドレスへの最適なルートを持つインターフェイスのインデックスを受け取る DWORD 変数へのポインター。 |
戻り値の型: DWORD
公式ドキュメント
GetBestInterface 関数は、指定した IPv4 アドレスへの最適なルートを持つインターフェイスのインデックスを取得します。
戻り値
関数が成功した場合、戻り値は NO_ERROR です。
関数が失敗した場合、戻り値は次のいずれかのエラーコードになります。
| 戻りコード | 説明 |
|---|---|
| 操作を完了できませんでした。 | |
| 無効なパラメーターが関数に渡されました。このエラーは、pdwBestIfIndex パラメーターに NULL ポインターが渡された場合、または pdwBestIfIndex が書き込み不可能なメモリを指している場合に返されます。 | |
| 要求はサポートされていません。このエラーは、ローカルコンピューターに IPv4 スタックが存在しない場合に返されます。 | |
|
FormatMessage 関数を使用して、返されたエラーに対応するメッセージ文字列を取得してください。 |
解説(Remarks)
GetBestInterface 関数は IPv4 アドレスでのみ動作します。IPv6 アドレスで使用する場合は、GetBestInterfaceEx を使用する必要があります。
IPAddr データ型については、Windows データ型を参照してください。IP アドレスをドット区切り 10 進表記と IPAddr 形式の間で変換するには、inet_addr 関数および inet_ntoa 関数を使用します。
Windows Vista 以降では、pdwBestIfIndex パラメーターは IP Helper によって内部的に NET_IFINDEX データ型へのポインターとして扱われます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 方式にも切替可。#uselib "IPHLPAPI.dll" #cfunc global GetBestInterface "GetBestInterface" int, sptr ; res = GetBestInterface(dwDestAddr, varptr(pdwBestIfIndex)) ; dwDestAddr : DWORD -> "int" ; pdwBestIfIndex : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; DWORD GetBestInterface(DWORD dwDestAddr, DWORD* pdwBestIfIndex) #uselib "IPHLPAPI.dll" #cfunc global GetBestInterface "GetBestInterface" int, intptr ; res = GetBestInterface(dwDestAddr, varptr(pdwBestIfIndex)) ; dwDestAddr : DWORD -> "int" ; pdwBestIfIndex : DWORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // DWORDfunction 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)。const std = @import("std");
extern "iphlpapi" fn GetBestInterface(
dwDestAddr: u32, // DWORD
pdwBestIfIndex: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) u32;proc GetBestInterface(
dwDestAddr: uint32, # DWORD
pdwBestIfIndex: ptr uint32 # DWORD* out
): uint32 {.importc: "GetBestInterface", stdcall, dynlib: "IPHLPAPI.dll".}pragma(lib, "iphlpapi");
extern(Windows)
uint GetBestInterface(
uint dwDestAddr, // DWORD
uint* pdwBestIfIndex // DWORD* out
);ccall((:GetBestInterface, "IPHLPAPI.dll"), stdcall, UInt32,
(UInt32, Ptr{UInt32}),
dwDestAddr, pdwBestIfIndex)
# dwDestAddr : DWORD -> UInt32
# pdwBestIfIndex : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetBestInterface(
uint32_t dwDestAddr,
uint32_t* pdwBestIfIndex);
]]
local iphlpapi = ffi.load("iphlpapi")
-- iphlpapi.GetBestInterface(dwDestAddr, pdwBestIfIndex)
-- dwDestAddr : DWORD
-- pdwBestIfIndex : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('IPHLPAPI.dll');
const GetBestInterface = lib.func('__stdcall', 'GetBestInterface', 'uint32_t', ['uint32_t', 'uint32_t *']);
// GetBestInterface(dwDestAddr, pdwBestIfIndex)
// dwDestAddr : DWORD -> 'uint32_t'
// pdwBestIfIndex : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("IPHLPAPI.dll", {
GetBestInterface: { parameters: ["u32", "pointer"], result: "u32" },
});
// lib.symbols.GetBestInterface(dwDestAddr, pdwBestIfIndex)
// dwDestAddr : DWORD -> "u32"
// pdwBestIfIndex : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetBestInterface(
uint32_t dwDestAddr,
uint32_t* pdwBestIfIndex);
C, "IPHLPAPI.dll");
// $ffi->GetBestInterface(dwDestAddr, pdwBestIfIndex);
// dwDestAddr : DWORD
// pdwBestIfIndex : DWORD* out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Iphlpapi extends StdCallLibrary {
Iphlpapi INSTANCE = Native.load("iphlpapi", Iphlpapi.class);
int GetBestInterface(
int dwDestAddr, // DWORD
IntByReference pdwBestIfIndex // DWORD* out
);
}@[Link("iphlpapi")]
lib LibIPHLPAPI
fun GetBestInterface = GetBestInterface(
dwDestAddr : UInt32, # DWORD
pdwBestIfIndex : UInt32* # DWORD* out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetBestInterfaceNative = Uint32 Function(Uint32, Pointer<Uint32>);
typedef GetBestInterfaceDart = int Function(int, Pointer<Uint32>);
final GetBestInterface = DynamicLibrary.open('IPHLPAPI.dll')
.lookupFunction<GetBestInterfaceNative, GetBestInterfaceDart>('GetBestInterface');
// dwDestAddr : DWORD -> Uint32
// pdwBestIfIndex : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetBestInterface(
dwDestAddr: DWORD; // DWORD
pdwBestIfIndex: Pointer // DWORD* out
): DWORD; stdcall;
external 'IPHLPAPI.dll' name 'GetBestInterface';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetBestInterface"
c_GetBestInterface :: Word32 -> Ptr Word32 -> IO Word32
-- dwDestAddr : DWORD -> Word32
-- pdwBestIfIndex : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getbestinterface =
foreign "GetBestInterface"
(uint32_t @-> (ptr uint32_t) @-> returning uint32_t)
(* dwDestAddr : DWORD -> uint32_t *)
(* pdwBestIfIndex : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library iphlpapi (t "IPHLPAPI.dll"))
(cffi:use-foreign-library iphlpapi)
(cffi:defcfun ("GetBestInterface" get-best-interface :convention :stdcall) :uint32
(dw-dest-addr :uint32) ; DWORD
(pdw-best-if-index :pointer)) ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetBestInterface = Win32::API::More->new('IPHLPAPI',
'DWORD GetBestInterface(DWORD dwDestAddr, LPVOID pdwBestIfIndex)');
# my $ret = $GetBestInterface->Call($dwDestAddr, $pdwBestIfIndex);
# dwDestAddr : DWORD -> DWORD
# pdwBestIfIndex : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
類似 API
- f GetBestInterfaceEx — 指定宛先ソケットアドレスへの最適なインターフェイス索引を取得する。
公式の関連項目
- f GetBestRoute — 指定宛先・送信元IPv4アドレスに最適な経路を取得する。
- s IN_ADDR
- s MIB_BEST_IF