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

GetAdapterIndex

関数
アダプター名から対応するインターフェイス索引を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD GetAdapterIndex(
    LPWSTR AdapterName,
    DWORD* IfIndex
);

パラメーター

名前方向
AdapterNameLPWSTRin
IfIndexDWORD*inout

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD GetAdapterIndex(
    LPWSTR AdapterName,
    DWORD* IfIndex
);
[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetAdapterIndex(
    [MarshalAs(UnmanagedType.LPWStr)] string AdapterName,   // LPWSTR
    ref uint IfIndex   // DWORD* in/out
);
<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetAdapterIndex(
    <MarshalAs(UnmanagedType.LPWStr)> AdapterName As String,   ' LPWSTR
    ByRef IfIndex As UInteger   ' DWORD* in/out
) As UInteger
End Function
' AdapterName : LPWSTR
' IfIndex : DWORD* in/out
Declare PtrSafe Function GetAdapterIndex Lib "iphlpapi" ( _
    ByVal AdapterName As LongPtr, _
    ByRef IfIndex As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetAdapterIndex = ctypes.windll.iphlpapi.GetAdapterIndex
GetAdapterIndex.restype = wintypes.DWORD
GetAdapterIndex.argtypes = [
    wintypes.LPCWSTR,  # AdapterName : LPWSTR
    ctypes.POINTER(wintypes.DWORD),  # IfIndex : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetAdapterIndex = iphlpapi.NewProc("GetAdapterIndex")
)

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