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

GetIfEntry2

関数
指定ネットワークインターフェイスの詳細情報を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

WIN32_ERROR GetIfEntry2(
    MIB_IF_ROW2* Row
);

パラメーター

名前方向説明
RowMIB_IF_ROW2*inoutMIB_IF_ROW2 構造体へのポインターです。関数が成功すると、ローカルコンピューター上のインターフェイスに関する情報を受け取ります。入力時には、情報を取得する対象のインターフェイスを示すために、MIB_IF_ROW2InterfaceLuid メンバーまたは InterfaceIndex メンバーを設定しておく必要があります。

戻り値の型: WIN32_ERROR

公式ドキュメント

ローカルコンピューター上の指定したインターフェイスに関する情報を取得します。

戻り値

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

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

戻り値 説明
ERROR_FILE_NOT_FOUND
システムは指定されたファイルを見つけられません。このエラーは、Row パラメーターが指す MIB_IF_ROW2InterfaceLuid メンバーまたは InterfaceIndex メンバーで指定したネットワークインターフェイスの LUID またはインターフェイスインデックスが、ローカルマシン上に存在しない値であった場合に返されます。
ERROR_INVALID_PARAMETER
無効なパラメーターが関数に渡されました。このエラーは、Row パラメーターに NULL が渡された場合に返されます。また、Row パラメーターが指す MIB_IF_ROW2InterfaceLuid メンバーと InterfaceIndex メンバーがいずれも指定されていない場合にも返されます。
その他
返されたエラーに対応するメッセージ文字列を取得するには、FormatMessage 関数を使用してください。

解説(Remarks)

GetIfEntry2 関数は Windows Vista 以降で定義されています。

入力時には、Row パラメーターに渡す MIB_IF_ROW2 構造体の次のメンバーのうち、少なくとも 1 つを初期化しておく必要があります。InterfaceLuid または InterfaceIndex です。

これらのフィールドは上記の順序で使用されます。したがって、InterfaceLuid が指定されている場合は、このメンバーを使ってインターフェイスを特定します。InterfaceLuid メンバーに値が設定されていない場合(このメンバーの値が 0 に設定されている場合)は、次に InterfaceIndex メンバーを使ってインターフェイスを特定します。

出力時には、Row パラメーターが指す MIB_IF_ROW2 構造体の残りのフィールドが設定されます。

なお、Netioapi.h ヘッダーファイルは Iphlpapi.h ヘッダーファイルによって自動的にインクルードされるため、直接使用してはいけません。

使用例

次の例では、コマンドラインで指定したインターフェイスのエントリを取得し、取得した MIB_IF_ROW2 構造体からいくつかの値を出力します。

#ifndef UNICODE
#define UNICODE
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <Windows.h>

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>

#include <objbase.h>
#include <wtypes.h>
#include <stdio.h>
#include <stdlib.h>

// Need to link with Iphlpapi.lib
#pragma comment(lib, "iphlpapi.lib")

// Need to link with Ole32.lib to print GUID
#pragma comment(lib, "ole32.lib")

void PrintIfEntry2(PMIB_IF_ROW2 pifRow);

int __cdecl wmain(int argc, WCHAR ** argv)
{

    // Declare and initialize variables

    ULONG retVal = 0;
    ULONG ifIndex;

    MIB_IF_ROW2 ifRow;

    // Make sure the ifRow is zeroed out
    SecureZeroMemory((PVOID) &ifRow, sizeof(MIB_IF_ROW2) );

    // Zero out the MIB_IF_ROW2 struct

    // Validate the parameters
    if (argc < 2) {
        wprintf(L"usage: %s <InterfaceIndex>\n", argv[0]);
        wprintf(L"   Gets the Interface Entry for an interface Index,\n");
        wprintf(L"Example to get the interface at interface index=6\n");
        wprintf(L"       %s 6\n", argv[0]);
        exit(1);
    }

    ifIndex = _wtoi(argv[1]);

    ifRow.InterfaceIndex = ifIndex;

    retVal = GetIfEntry2(&ifRow);

    if (retVal != NO_ERROR) {
        wprintf(L"GetIfEntry returned error: %lu\n", retVal);
        exit(1);
    }
    else
        wprintf(L"GetIfEntry2 function returned okay\n");
    
    PrintIfEntry2(&ifRow);

    exit(0);
}

// Print some parameters from the MIB_IF_ROW2 structure
void PrintIfEntry2(PMIB_IF_ROW2 pIfRow)
{

    int iRet = 0;
    WCHAR GuidString[40] = { 0 };

    unsigned int j;

    wprintf(L"\tInterfaceIndex:\t %lu\n", pIfRow->InterfaceIndex);

    iRet = StringFromGUID2(pIfRow->InterfaceGuid, (LPOLESTR) & GuidString, 39);
    // For c rather than C++ source code, the above line needs to be
    // iRet = StringFromGUID2(&pIfRow->InterfaceGuid, (LPOLESTR) &GuidString, 39); 
    if (iRet == 0)
        wprintf(L"StringFromGUID2 failed\n");
    else {
        wprintf(L"\tInterfaceGUID:   %ws\n", GuidString);
    }

    wprintf(L"\tAlias:\t\t %ws", pIfRow->Alias);
    wprintf(L"\n");
    wprintf(L"\tDescription:\t %ws", pIfRow->Description);
    wprintf(L"\n");
    wprintf(L"\tPhysical Address:\t    ");
    if (pIfRow->PhysicalAddressLength == 0)
        wprintf(L"\n");
    for (j = 0; j < (int) pIfRow->PhysicalAddressLength; j++) {
        if (j == (pIfRow->PhysicalAddressLength - 1))
            wprintf(L"%.2X\n", (int) pIfRow->PhysicalAddress[j]);
        else
            wprintf(L"%.2X-", (int) pIfRow->PhysicalAddress[j]);
    }
    wprintf(L"\tPermanent Physical Address: ");
    if (pIfRow->PhysicalAddressLength == 0)
        wprintf(L"\n");
    for (j = 0; j < (int) pIfRow->PhysicalAddressLength; j++) {
        if (j == (pIfRow->PhysicalAddressLength - 1))
            wprintf(L"%.2X\n", (int) pIfRow->PermanentPhysicalAddress[j]);
        else
            wprintf(L"%.2X-", (int) pIfRow->PermanentPhysicalAddress[j]);
    }
    wprintf(L"\tMtu:\t\t %lu\n", pIfRow->Mtu);

    wprintf(L"\tType:\t\t ");
    switch (pIfRow->Type) {
    case IF_TYPE_OTHER:
        wprintf(L"Other\n");
        break;
    case IF_TYPE_ETHERNET_CSMACD:
        wprintf(L"Ethernet\n");
        break;
    case IF_TYPE_ISO88025_TOKENRING:
        wprintf(L"Token Ring\n");
        break;
    case IF_TYPE_PPP:
        wprintf(L"PPP\n");
        break;
    case IF_TYPE_SOFTWARE_LOOPBACK:
        wprintf(L"Software Lookback\n");
        break;
    case IF_TYPE_ATM:
        wprintf(L"ATM\n");
        break;
    case IF_TYPE_IEEE80211:
        wprintf(L"IEEE 802.11 Wireless\n");
        break;
    case IF_TYPE_TUNNEL:
        wprintf(L"Tunnel type encapsulation\n");
        break;
    case IF_TYPE_IEEE1394:
        wprintf(L"IEEE 1394 Firewire\n");
        break;
    default:
        wprintf(L"Unknown type %ld\n", pIfRow->Type);
        break;
    }

    wprintf(L"\tTunnel Type:\t ");
    switch (pIfRow->TunnelType) {
    case TUNNEL_TYPE_NONE:
        wprintf(L"Not a tunnel\n");
        break;
    case TUNNEL_TYPE_OTHER:
        wprintf(L"None of the known tunnel types\n");
        break;
    case TUNNEL_TYPE_DIRECT:
        wprintf(L"Encapsulated directly within IPv4\n");
        break;
    case TUNNEL_TYPE_6TO4:
        wprintf
            (L"IPv6 packet encapsulated within IPv4 using 6to4 protocol\n");
        break;
    case TUNNEL_TYPE_ISATAP:
        wprintf
            (L"IPv6 packet encapsulated within IPv4 using ISATAP protocol\n");
        break;
    case TUNNEL_TYPE_TEREDO:
        wprintf(L"Teredo encapsulation\n");
        break;
    default:
        wprintf(L"Unknown tunnel type %ld\n", pIfRow->TunnelType);
        break;
    }

    wprintf(L"\tNDIS Media Type:\t ");
    switch (pIfRow->MediaType) {
    case NdisMedium802_3:
        wprintf(L"Ethernet (802.3)\n");
        break;
    case NdisMedium802_5:
        wprintf(L"Token Ring (802.5)\n");
        break;
    case NdisMediumFddi:
        wprintf(L"Fiber Distributed Data Interface (FDDI)\n");
        break;
    case NdisMediumWan:
        wprintf(L"Wide area network (WAN)\n");
        break;
    case NdisMediumLocalTalk:
        wprintf(L"LocalTalk\n");
        break;
    case NdisMediumDix:
        wprintf(L"Ethernet using DIX header format\n");
        break;
    case NdisMediumArcnetRaw:
        wprintf(L"ARCNET\n");
        break;
    case NdisMediumArcnet878_2:
        wprintf(L"ARCNET (878.2)\n");
        break;
    case NdisMediumAtm:
        wprintf(L"ATM\n");
        break;
    case NdisMediumWirelessWan:
        wprintf(L"Wireless WAN\n");
        break;
    case NdisMediumIrda:
        wprintf(L"Infrared (IrDA)\n");
        break;
    case NdisMediumBpc:
        wprintf(L"Broadcast PC\n");
        break;
    case NdisMediumCoWan:
        wprintf(L"Connection-oriented Wide Area Network (CoWAN)\n");
        break;
    case NdisMedium1394:
        wprintf(L"IEEE 1394 (fire wire)\n");
        break;
    case NdisMediumInfiniBand:
        wprintf(L"InfiniBand\n");
        break;
    case NdisMediumTunnel:
        wprintf(L"A Tunnel\n");
        break;
    case NdisMediumNative802_11:
        wprintf(L"Native IEEE 802.11\n");
        break;
    case NdisMediumLoopback:
        wprintf(L"NDIS loopback \n");
        break;
    default:
        wprintf(L"Unknown media type %ld\n", pIfRow->MediaType);
        break;
    }

    printf("\tAdministrative Status:\t ");
    switch (pIfRow->AdminStatus) {
    case NET_IF_ADMIN_STATUS_UP:
        wprintf(L"Interface up and enabled\n");
        break;
    case NET_IF_ADMIN_STATUS_DOWN:
        wprintf(L"Interface down\n");
        break;
    case NET_IF_ADMIN_STATUS_TESTING:
        wprintf(L"Interface in test mode\n");
        break;
    default:
        wprintf(L"Unknown status %ld\n", pIfRow->AdminStatus);
        break;
    }

    printf("\tMedia connection state:\t ");
    switch (pIfRow->MediaConnectState) {
    case MediaConnectStateUnknown:
        wprintf(L"Interface state is unknown\n");
        break;
    case MediaConnectStateConnected:
        wprintf(L"Connected\n");
        break;
    case MediaConnectStateDisconnected:
        wprintf(L"Disconnected\n");
        break;
    default:
        wprintf(L"Unknown state %ld\n", pIfRow->MediaConnectState);
        break;
    }

    wprintf(L"\tTransmit link speed:\t %I64u\n", pIfRow->TransmitLinkSpeed);
    wprintf(L"\tReceive link speed:\t %I64u\n", pIfRow->ReceiveLinkSpeed);

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

各言語での呼び出し定義

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

WIN32_ERROR GetIfEntry2(
    MIB_IF_ROW2* Row
);
[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetIfEntry2(
    IntPtr Row   // MIB_IF_ROW2* in/out
);
<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetIfEntry2(
    Row As IntPtr   ' MIB_IF_ROW2* in/out
) As UInteger
End Function
' Row : MIB_IF_ROW2* in/out
Declare PtrSafe Function GetIfEntry2 Lib "iphlpapi" ( _
    ByVal Row As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetIfEntry2 = ctypes.windll.iphlpapi.GetIfEntry2
GetIfEntry2.restype = wintypes.DWORD
GetIfEntry2.argtypes = [
    ctypes.c_void_p,  # Row : MIB_IF_ROW2* in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetIfEntry2 = iphlpapi.NewProc("GetIfEntry2")
)

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

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

typedef GetIfEntry2Native = Uint32 Function(Pointer<Void>);
typedef GetIfEntry2Dart = int Function(Pointer<Void>);
final GetIfEntry2 = DynamicLibrary.open('IPHLPAPI.dll')
    .lookupFunction<GetIfEntry2Native, GetIfEntry2Dart>('GetIfEntry2');
// Row : MIB_IF_ROW2* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetIfEntry2(
  Row: Pointer   // MIB_IF_ROW2* in/out
): DWORD; stdcall;
  external 'IPHLPAPI.dll' name 'GetIfEntry2';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetIfEntry2"
  c_GetIfEntry2 :: Ptr () -> IO Word32
-- Row : MIB_IF_ROW2* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getifentry2 =
  foreign "GetIfEntry2"
    ((ptr void) @-> returning uint32_t)
(* Row : MIB_IF_ROW2* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library iphlpapi (t "IPHLPAPI.dll"))
(cffi:use-foreign-library iphlpapi)

(cffi:defcfun ("GetIfEntry2" get-if-entry2 :convention :stdcall) :uint32
  (row :pointer))   ; MIB_IF_ROW2* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetIfEntry2 = Win32::API::More->new('IPHLPAPI',
    'DWORD GetIfEntry2(LPVOID Row)');
# my $ret = $GetIfEntry2->Call($Row);
# Row : MIB_IF_ROW2* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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