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

if_indextoname

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

シグネチャ

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

LPSTR if_indextoname(
    DWORD InterfaceIndex,
    LPSTR InterfaceName
);

パラメーター

名前方向説明
InterfaceIndexDWORDinネットワークインターフェイスのローカルインデックス。
InterfaceNameLPSTRout関数が正常に終了したときに、インターフェイス名を格納する NULL 終端 ANSI 文字列を保持するバッファへのポインター。このパラメーターが指すバッファの長さ (バイト単位) は、 IF_NAMESIZE 以上である必要があります。

戻り値の型: LPSTR

公式ドキュメント

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

戻り値

成功した場合、 if_indextoname はインターフェイス名を格納した NULL 終端 ANSI 文字列へのポインターを返します。失敗した場合は NULL ポインターが返されます。

解説(Remarks)

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

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

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

if_indextoname が失敗して NULL ポインターを返した場合、エラーコードを特定することはできません。

InterfaceName パラメーターが指すバッファの長さ (バイト単位) は、IF_NAMESIZE 以上である必要があります。IF_NAMESIZENetioapi.h ヘッダーファイルで宣言される値で、NDIS_IF_MAX_STRING_SIZE と等しくなります。終端の NULL を含まないインターフェイス名の最大長 NDIS_IF_MAX_STRING_SIZE は、Ntddndis.h ヘッダーファイルで宣言されています。NDIS_IF_MAX_STRING_SIZE は、Ifdef.h ヘッダーファイルで定義されている IF_MAX_STRING_SIZE 定数として定義されています。Ntddndis.h および Ifdef.h ヘッダーファイルは Netioapi.h ヘッダーファイルに自動的にインクルードされ、Netioapi.hIphlpapi.h ヘッダーファイルに自動的にインクルードされます。Ntddndis.hIfdef.h、および Netioapi.h の各ヘッダーファイルを直接使用しないでください。

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

各言語での呼び出し定義

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

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

if_indextoname = ctypes.windll.iphlpapi.if_indextoname
if_indextoname.restype = wintypes.LPSTR
if_indextoname.argtypes = [
    wintypes.DWORD,  # InterfaceIndex : DWORD
    wintypes.LPSTR,  # InterfaceName : LPSTR out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procif_indextoname = iphlpapi.NewProc("if_indextoname")
)

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

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

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

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

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

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

関連項目

公式の関連項目