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

GetBestInterfaceEx

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

シグネチャ

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

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

パラメーター

名前方向説明
pDestAddrSOCKADDR*in最適な経路を持つインターフェイスを取得する対象となる宛先 IPv6 または IPv4 アドレス。sockaddr 構造体の形式で指定します。
pdwBestIfIndexDWORD*outpDestAddr で指定された IPv6 または IPv4 アドレスへの最適な経路を持つインターフェイスのインデックスへのポインター。

戻り値の型: DWORD

公式ドキュメント

GetBestInterfaceEx 関数は、指定された IPv4 または IPv6 アドレスへの最適な経路を持つインターフェイスのインデックスを取得します。

戻り値

関数が成功した場合、戻り値は NO_ERROR です。

関数が失敗した場合、戻り値は次のいずれかのエラーコードになります。

戻り値 説明
ERROR_CAN_NOT_COMPLETE
操作を完了できませんでした。
ERROR_INVALID_PARAMETER
無効なパラメーターが関数に渡されました。このエラーは、pdwBestIfIndex パラメーターに NULL ポインターが渡された場合、または pDestAddr もしくは pdwBestIfIndex パラメーターがアクセスできないメモリを指している場合に返されます。また、pdwBestIfIndex パラメーターが書き込みできないメモリを指している場合にも返されることがあります。
ERROR_NOT_SUPPORTED
要求はサポートされていません。このエラーは、ローカルコンピューターに IPv4 スタックが存在しないのに pDestAddr パラメーターに IPv4 アドレスが指定された場合、またはローカルコンピューターに IPv6 スタックが存在しないのに pDestAddr パラメーターに IPv6 アドレスが指定された場合に返されます。
その他
FormatMessage 関数を使用して、返されたエラーに対応するメッセージ文字列を取得してください。

解説(Remarks)

GetBestInterfaceEx 関数は、IPv4 アドレスと IPv6 アドレスのどちらにも使用できる点で GetBestInterface 関数と異なります。

pDestAddr パラメーターが指す sockaddr 構造体の Family メンバーには、次のいずれかの値を設定する必要があります: AF_INET または AF_INET6

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)

各言語での呼び出し定義

// 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)。
const std = @import("std");

extern "iphlpapi" fn GetBestInterfaceEx(
    pDestAddr: [*c]SOCKADDR, // SOCKADDR*
    pdwBestIfIndex: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) u32;
proc GetBestInterfaceEx(
    pDestAddr: ptr SOCKADDR,  # SOCKADDR*
    pdwBestIfIndex: ptr uint32  # DWORD* out
): uint32 {.importc: "GetBestInterfaceEx", stdcall, dynlib: "IPHLPAPI.dll".}
pragma(lib, "iphlpapi");
extern(Windows)
uint GetBestInterfaceEx(
    SOCKADDR* pDestAddr,   // SOCKADDR*
    uint* pdwBestIfIndex   // DWORD* out
);
ccall((:GetBestInterfaceEx, "IPHLPAPI.dll"), stdcall, UInt32,
      (Ptr{SOCKADDR}, Ptr{UInt32}),
      pDestAddr, pdwBestIfIndex)
# pDestAddr : SOCKADDR* -> Ptr{SOCKADDR}
# pdwBestIfIndex : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetBestInterfaceEx(
    void* pDestAddr,
    uint32_t* pdwBestIfIndex);
]]
local iphlpapi = ffi.load("iphlpapi")
-- iphlpapi.GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
-- pDestAddr : SOCKADDR*
-- pdwBestIfIndex : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('IPHLPAPI.dll');
const GetBestInterfaceEx = lib.func('__stdcall', 'GetBestInterfaceEx', 'uint32_t', ['void *', 'uint32_t *']);
// GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
// pDestAddr : SOCKADDR* -> 'void *'
// pdwBestIfIndex : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("IPHLPAPI.dll", {
  GetBestInterfaceEx: { parameters: ["pointer", "pointer"], result: "u32" },
});
// lib.symbols.GetBestInterfaceEx(pDestAddr, pdwBestIfIndex)
// pDestAddr : SOCKADDR* -> "pointer"
// pdwBestIfIndex : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetBestInterfaceEx(
    void* pDestAddr,
    uint32_t* pdwBestIfIndex);
C, "IPHLPAPI.dll");
// $ffi->GetBestInterfaceEx(pDestAddr, pdwBestIfIndex);
// pDestAddr : SOCKADDR*
// 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 GetBestInterfaceEx(
        Pointer pDestAddr,   // SOCKADDR*
        IntByReference pdwBestIfIndex   // DWORD* out
    );
}
@[Link("iphlpapi")]
lib LibIPHLPAPI
  fun GetBestInterfaceEx = GetBestInterfaceEx(
    pDestAddr : SOCKADDR*,   # SOCKADDR*
    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 GetBestInterfaceExNative = Uint32 Function(Pointer<Void>, Pointer<Uint32>);
typedef GetBestInterfaceExDart = int Function(Pointer<Void>, Pointer<Uint32>);
final GetBestInterfaceEx = DynamicLibrary.open('IPHLPAPI.dll')
    .lookupFunction<GetBestInterfaceExNative, GetBestInterfaceExDart>('GetBestInterfaceEx');
// pDestAddr : SOCKADDR* -> Pointer<Void>
// pdwBestIfIndex : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetBestInterfaceEx(
  pDestAddr: Pointer;   // SOCKADDR*
  pdwBestIfIndex: Pointer   // DWORD* out
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'GetBestInterfaceEx';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetBestInterfaceEx"
  c_GetBestInterfaceEx :: Ptr () -> Ptr Word32 -> IO Word32
-- pDestAddr : SOCKADDR* -> Ptr ()
-- pdwBestIfIndex : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getbestinterfaceex =
  foreign "GetBestInterfaceEx"
    ((ptr void) @-> (ptr uint32_t) @-> returning uint32_t)
(* pDestAddr : SOCKADDR* -> (ptr void) *)
(* 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 ("GetBestInterfaceEx" get-best-interface-ex :convention :stdcall) :uint32
  (p-dest-addr :pointer)   ; SOCKADDR*
  (pdw-best-if-index :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetBestInterfaceEx = Win32::API::More->new('IPHLPAPI',
    'DWORD GetBestInterfaceEx(LPVOID pDestAddr, LPVOID pdwBestIfIndex)');
# my $ret = $GetBestInterfaceEx->Call($pDestAddr, $pdwBestIfIndex);
# pDestAddr : SOCKADDR* -> LPVOID
# pdwBestIfIndex : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目
使用する型