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

GetTcp6Table

関数
現在のIPv6 TCP接続一覧をMIB_TCP6TABLEとして取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD GetTcp6Table(
    MIB_TCP6TABLE* TcpTable,
    DWORD* SizePointer,
    BOOL Order
);

パラメーター

名前方向説明
TcpTableMIB_TCP6TABLE*outIPv6 用の TCP 接続テーブルを MIB_TCP6TABLE 構造体として受け取るバッファーへのポインター。
SizePointerDWORD*inout

入力時には、TcpTable パラメーターが指すバッファーのサイズをバイト単位で指定します。

出力時には、返される TCP 接続テーブルを格納するのにバッファーが十分な大きさでない場合、この関数はこのパラメーターに必要なバッファーサイズ(バイト単位)を設定します。

OrderBOOLin

TCP 接続テーブルをソートするかどうかを指定するブール値。このパラメーターが TRUE の場合、テーブルは最も小さいローカル IP アドレスを先頭として昇順でソートされます。このパラメーターが FALSE の場合、テーブルは取得された順序で並びます。

TCP エンドポイントを並べ替える際には、次の値が(列挙順に)比較されます。

  1. ローカル IPv6 アドレス
  2. ローカルスコープ ID
  3. ローカルポート
  4. リモート IPv6 アドレス
  5. リモートスコープ ID
  6. リモートポート

戻り値の型: DWORD

公式ドキュメント

IPv6 用の TCP 接続テーブルを取得します。(GetTcp6Table)

戻り値

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

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

戻り値 説明
ERROR_INSUFFICIENT_BUFFER
TcpTable パラメーターが指すバッファーが十分な大きさではありません。必要なサイズは SizePointer パラメーターが指す変数に返されます。
ERROR_INVALID_PARAMETER
SizePointer パラメーターが NULL であるか、 GetTcp6TableSizePointer パラメーターの指すメモリーに書き込めません。
ERROR_NOT_SUPPORTED
この関数は、ローカルシステムで使用されているオペレーティングシステムではサポートされていません。
その他
返されたエラーに対応するメッセージ文字列を取得するには、 FormatMessage を使用してください。

解説(Remarks)

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

次の例は、IPv6 用の TCP 接続テーブルを取得し、各接続の状態を出力します。

#ifndef UNICODE
#define UNICODE
#endif

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

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

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */

int wmain()
{

    // Declare and initialize variables
    PMIB_TCP6TABLE pTcpTable;
    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    wchar_t ipstringbuffer[46];
    
    int i;

    pTcpTable = (MIB_TCP6TABLE *) MALLOC(sizeof (MIB_TCP6TABLE));
    if (pTcpTable == NULL) {
        wprintf(L"Error allocating memory\n");
        return 1;
    }

    dwSize = sizeof (MIB_TCP6TABLE);
// Make an initial call to GetTcp6Table to
// get the necessary size into the dwSize variable
    if ((dwRetVal = GetTcp6Table(pTcpTable, &dwSize, TRUE)) ==
        ERROR_INSUFFICIENT_BUFFER) {
        FREE(pTcpTable);
        pTcpTable = (MIB_TCP6TABLE *) MALLOC(dwSize);
        if (pTcpTable == NULL) {
            wprintf(L"Error allocating memory\n");
            return 1;
        }
    }
// Make a second call to GetTcp6Table to get
// the actual data we require
    if ((dwRetVal = GetTcp6Table(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
        wprintf(L"\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
        for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
            wprintf(L"\n\tTCP[%d] State: %ld - ", i,
                   pTcpTable->table[i].State);
            switch (pTcpTable->table[i].State) {
            case MIB_TCP_STATE_CLOSED:
                wprintf(L"CLOSED\n");
                break;
            case MIB_TCP_STATE_LISTEN:
                wprintf(L"LISTEN\n");
                break;
            case MIB_TCP_STATE_SYN_SENT:
                wprintf(L"SYN-SENT\n");
                break;
            case MIB_TCP_STATE_SYN_RCVD:
                wprintf(L"SYN-RECEIVED\n");
                break;
            case MIB_TCP_STATE_ESTAB:
                wprintf(L"ESTABLISHED\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT1:
                wprintf(L"FIN-WAIT-1\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT2:
                wprintf(L"FIN-WAIT-2 \n");
                break;
            case MIB_TCP_STATE_CLOSE_WAIT:
                wprintf(L"CLOSE-WAIT\n");
                break;
            case MIB_TCP_STATE_CLOSING:
                wprintf(L"CLOSING\n");
                break;
            case MIB_TCP_STATE_LAST_ACK:
                wprintf(L"LAST-ACK\n");
                break;
            case MIB_TCP_STATE_TIME_WAIT:
                wprintf(L"TIME-WAIT\n");
                break;
            case MIB_TCP_STATE_DELETE_TCB:
                wprintf(L"DELETE-TCB\n");
                break;
            default:
                wprintf(L"UNKNOWN dwState value\n");
                break;
            }

            if (InetNtop(AF_INET6, &pTcpTable->table[i].LocalAddr, ipstringbuffer, 46) == NULL)
                wprintf(L"  InetNtop function failed for local IPv6 address\n");
            else     
                wprintf(L"\tTCP[%d] Local Addr: %s\n", i, ipstringbuffer);
            wprintf(L"\tTCP[%d] Local Scope ID: %d \n", i,
                   ntohl (pTcpTable->table[i].dwLocalScopeId));
            wprintf(L"\tTCP[%d] Local Port: %d \n", i,
                   ntohs((u_short)pTcpTable->table[i].dwLocalPort));

            if (InetNtop(AF_INET6, &pTcpTable->table[i].RemoteAddr, ipstringbuffer, 46) == NULL)
                wprintf(L"  InetNtop function failed for remote IPv6 address\n");
            else     
                wprintf(L"\tTCP[%d] Remote Addr: %s\n", i, ipstringbuffer);
            wprintf(L"\tTCP[%d] Remote Scope ID: %d \n", i,
                   ntohl(pTcpTable->table[i].dwRemoteScopeId));
            wprintf(L"\tTCP[%d] Remote Port: %d\n", i,
                   ntohs((u_short)pTcpTable->table[i].dwRemotePort));
        }
    } else {
        wprintf(L"\tGetTcp6Table failed with %d\n", dwRetVal);
        FREE(pTcpTable);
        return 1;
    }

    if (pTcpTable != NULL) {
        FREE(pTcpTable);
        pTcpTable = NULL;
    }
    
    return 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 GetTcp6Table(
    MIB_TCP6TABLE* TcpTable,
    DWORD* SizePointer,
    BOOL Order
);
[DllImport("IPHLPAPI.dll", ExactSpelling = true)]
static extern uint GetTcp6Table(
    IntPtr TcpTable,   // MIB_TCP6TABLE* out
    ref uint SizePointer,   // DWORD* in/out
    bool Order   // BOOL
);
<DllImport("IPHLPAPI.dll", ExactSpelling:=True)>
Public Shared Function GetTcp6Table(
    TcpTable As IntPtr,   ' MIB_TCP6TABLE* out
    ByRef SizePointer As UInteger,   ' DWORD* in/out
    Order As Boolean   ' BOOL
) As UInteger
End Function
' TcpTable : MIB_TCP6TABLE* out
' SizePointer : DWORD* in/out
' Order : BOOL
Declare PtrSafe Function GetTcp6Table Lib "iphlpapi" ( _
    ByVal TcpTable As LongPtr, _
    ByRef SizePointer As Long, _
    ByVal Order As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetTcp6Table = ctypes.windll.iphlpapi.GetTcp6Table
GetTcp6Table.restype = wintypes.DWORD
GetTcp6Table.argtypes = [
    ctypes.c_void_p,  # TcpTable : MIB_TCP6TABLE* out
    ctypes.POINTER(wintypes.DWORD),  # SizePointer : DWORD* in/out
    wintypes.BOOL,  # Order : BOOL
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetTcp6Table = iphlpapi.NewProc("GetTcp6Table")
)

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

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

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

foreign import stdcall safe "GetTcp6Table"
  c_GetTcp6Table :: Ptr () -> Ptr Word32 -> CInt -> IO Word32
-- TcpTable : MIB_TCP6TABLE* out -> Ptr ()
-- SizePointer : DWORD* in/out -> Ptr Word32
-- Order : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let gettcp6table =
  foreign "GetTcp6Table"
    ((ptr void) @-> (ptr uint32_t) @-> int32_t @-> returning uint32_t)
(* TcpTable : MIB_TCP6TABLE* out -> (ptr void) *)
(* SizePointer : DWORD* in/out -> (ptr uint32_t) *)
(* Order : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library iphlpapi (t "IPHLPAPI.dll"))
(cffi:use-foreign-library iphlpapi)

(cffi:defcfun ("GetTcp6Table" get-tcp6-table :convention :stdcall) :uint32
  (tcp-table :pointer)   ; MIB_TCP6TABLE* out
  (size-pointer :pointer)   ; DWORD* in/out
  (order :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetTcp6Table = Win32::API::More->new('IPHLPAPI',
    'DWORD GetTcp6Table(LPVOID TcpTable, LPVOID SizePointer, BOOL Order)');
# my $ret = $GetTcp6Table->Call($TcpTable, $SizePointer, $Order);
# TcpTable : MIB_TCP6TABLE* out -> LPVOID
# SizePointer : DWORD* in/out -> LPVOID
# Order : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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