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

if_nametoindex

関数
ネットワークインターフェイス名を対応するインターフェイスインデックスに変換する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD if_nametoindex(
    LPCSTR InterfaceName
);

パラメーター

名前方向
InterfaceNameLPCSTRin

戻り値の型: DWORD

各言語での呼び出し定義

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

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

if_nametoindex = ctypes.windll.iphlpapi.if_nametoindex
if_nametoindex.restype = wintypes.DWORD
if_nametoindex.argtypes = [
    wintypes.LPCSTR,  # InterfaceName : LPCSTR
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('IPHLPAPI.dll')
if_nametoindex = Fiddle::Function.new(
  lib['if_nametoindex'],
  [
    Fiddle::TYPE_VOIDP,  # InterfaceName : LPCSTR
  ],
  -Fiddle::TYPE_INT)
#[link(name = "iphlpapi")]
extern "system" {
    fn if_nametoindex(
        InterfaceName: *const u8  // LPCSTR
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("IPHLPAPI.dll")]
public static extern uint if_nametoindex([MarshalAs(UnmanagedType.LPStr)] string InterfaceName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'IPHLPAPI_if_nametoindex' -Namespace Win32 -PassThru
# $api::if_nametoindex(InterfaceName)
#uselib "IPHLPAPI.dll"
#func global if_nametoindex "if_nametoindex" sptr
; if_nametoindex InterfaceName   ; 戻り値は stat
; InterfaceName : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "IPHLPAPI.dll"
#cfunc global if_nametoindex "if_nametoindex" str
; res = if_nametoindex(InterfaceName)
; InterfaceName : LPCSTR -> "str"
; DWORD if_nametoindex(LPCSTR InterfaceName)
#uselib "IPHLPAPI.dll"
#cfunc global if_nametoindex "if_nametoindex" str
; res = if_nametoindex(InterfaceName)
; InterfaceName : LPCSTR -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procif_nametoindex = iphlpapi.NewProc("if_nametoindex")
)

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