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

if_nametoindex

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

シグネチャ

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

DWORD if_nametoindex(
    LPCSTR InterfaceName
);

パラメーター

名前方向説明
InterfaceNameLPCSTRinインターフェイス名を含む NULL 終端 ANSI 文字列へのポインター。

戻り値の型: DWORD

公式ドキュメント

ネットワークインターフェイスの ANSI インターフェイス名を、そのインターフェイスのローカルインデックスに変換します。

戻り値

成功した場合、 if_nametoindex はローカルインターフェイスインデックスを返します。失敗した場合は 0 が返されます。

解説(Remarks)

if_nametoindex 関数は Windows Vista 以降で使用できます。

if_nametoindex 関数は、インターフェイス名を対応するインデックスにマッピングします。この関数は、RFC 2553 で IETF によって規定された IPv6 向けの基本的なソケット拡張の一部として設計されています。詳細については、http://www.ietf.org/rfc/rfc2553.txt を参照してください。

if_nametoindex 関数は Unix 環境とのアプリケーションの移植性のために実装されていますが、ConvertInterface 系の関数の使用が推奨されます。if_nametoindex 関数は、ANSI インターフェイス名を NET_LUID に変換する ConvertInterfaceNameToLuidA 関数の呼び出しと、その NET_LUID をローカルインターフェイスインデックスに変換する ConvertInterfaceLuidToIndex の呼び出しに置き換えることができます。

if_nametoindex 関数が失敗して 0 を返した場合、エラーコードを特定することはできません。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

extern "iphlpapi" fn if_nametoindex(
    InterfaceName: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) u32;
proc if_nametoindex(
    InterfaceName: cstring  # LPCSTR
): uint32 {.importc: "if_nametoindex", stdcall, dynlib: "IPHLPAPI.dll".}
pragma(lib, "iphlpapi");
extern(Windows)
uint if_nametoindex(
    const(char)* InterfaceName   // LPCSTR
);
ccall((:if_nametoindex, "IPHLPAPI.dll"), stdcall, UInt32,
      (Cstring,),
      InterfaceName)
# InterfaceName : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t if_nametoindex(
    const char* InterfaceName);
]]
local iphlpapi = ffi.load("iphlpapi")
-- iphlpapi.if_nametoindex(InterfaceName)
-- InterfaceName : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('IPHLPAPI.dll');
const if_nametoindex = lib.func('__stdcall', 'if_nametoindex', 'uint32_t', ['str']);
// if_nametoindex(InterfaceName)
// InterfaceName : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("IPHLPAPI.dll", {
  if_nametoindex: { parameters: ["buffer"], result: "u32" },
});
// lib.symbols.if_nametoindex(InterfaceName)
// InterfaceName : LPCSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t if_nametoindex(
    const char* InterfaceName);
C, "IPHLPAPI.dll");
// $ffi->if_nametoindex(InterfaceName);
// InterfaceName : LPCSTR
// 構造体/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 if_nametoindex(
        String InterfaceName   // LPCSTR
    );
}
@[Link("iphlpapi")]
lib LibIPHLPAPI
  fun if_nametoindex = if_nametoindex(
    InterfaceName : UInt8*   # LPCSTR
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef if_nametoindexNative = Uint32 Function(Pointer<Utf8>);
typedef if_nametoindexDart = int Function(Pointer<Utf8>);
final if_nametoindex = DynamicLibrary.open('IPHLPAPI.dll')
    .lookupFunction<if_nametoindexNative, if_nametoindexDart>('if_nametoindex');
// InterfaceName : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function if_nametoindex(
  InterfaceName: PAnsiChar   // LPCSTR
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'if_nametoindex';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "if_nametoindex"
  c_if_nametoindex :: CString -> IO Word32
-- InterfaceName : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let if_nametoindex =
  foreign "if_nametoindex"
    (string @-> returning uint32_t)
(* InterfaceName : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library iphlpapi (t "IPHLPAPI.dll"))
(cffi:use-foreign-library iphlpapi)

(cffi:defcfun ("if_nametoindex" if-nametoindex :convention :stdcall) :uint32
  (interface-name :string))   ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $if_nametoindex = Win32::API::More->new('IPHLPAPI',
    'DWORD if_nametoindex(LPCSTR InterfaceName)');
# my $ret = $if_nametoindex->Call($InterfaceName);
# InterfaceName : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目