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

MIB_TCPTABLE

構造体
サイズx64: 24 バイト / x86: 24 バイト

サイズ=各フィールドのバイト数(x64/x86 で異なる場合は x64/x86 と併記)。x64/x86 列=フィールドのバイトオフセット(HSPで dupptr / lpoke / wpoke 等に使用)。

フィールド

フィールドサイズx64x86説明
dwNumEntriesDWORD4+0+0テーブル内のエントリ数。
tableMIB_TCPROW_LH20+4+4MIB_TCPROW 構造体の配列として実装された、TCP 接続のテーブルへのポインター。

公式ドキュメント

MIB_TCPTABLE 構造体は、ローカルコンピューター上の IPv4 の TCP 接続テーブルを格納します。

解説(Remarks)

GetTcpTable 関数は、ローカルコンピューター上の IPv4 TCP 接続テーブルを取得し、その情報を MIB_TCPTABLE 構造体で返します。MIB_TCPTABLE 構造体には、MIB_TCPROW 構造体の配列が格納されます。

MIB_TCPTABLE 構造体には、dwNumEntries メンバーと table メンバー内の最初の MIB_TCPROW 配列エントリとの間に、アラインメントのためのパディングが含まれる場合があります。また、table メンバー内の MIB_TCPROW 配列エントリの間にも、アラインメントのためのパディングが存在する場合があります。MIB_TCPROW 配列エントリにアクセスする際は、パディングが存在する可能性があることを前提としてください。

Windows Vista 以降向けにリリースされた Microsoft Windows Software Development Kit (SDK) では、ヘッダーファイルの構成が変更されています。この構造体は Iprtrmib.h ヘッダーファイルではなく、Tcpmib.h ヘッダーファイルで定義されています。Tcpmib.h ヘッダーファイルは Iprtrmib.h に自動的にインクルードされ、Iprtrmib.hIphlpapi.h ヘッダーファイルに自動的にインクルードされます。Tcpmib.h および Iprtrmib.h ヘッダーファイルを直接使用しないでください。

次の例では、IPv4 の TCP 接続テーブルを MIB_TCPTABLE 構造体として取得し、各接続を表す MIB_TCPROW 構造体の状態を出力します。

#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 main()
{

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

    char szLocalAddr[128];
    char szRemoteAddr[128];

    struct in_addr IpAddr;

    int i;

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

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

            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
            strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
            printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);

            printf("\tTCP[%d] Local Port: %d \n", i,
                   ntohs((u_short)pTcpTable->table[i].dwLocalPort));

            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
            strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
            printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);

            printf("\tTCP[%d] Remote Port: %d\n", i,
                   ntohs((u_short)pTcpTable->table[i].dwRemotePort));
        }
    } else {
        printf("\tGetTcpTable 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)

各言語での定義

#include <windows.h>

// MIB_TCPROW_LH  (x64 20 / x86 20 バイト)
typedef struct MIB_TCPROW_LH {
    _Anonymous_e__Union Anonymous;
    DWORD dwLocalAddr;
    DWORD dwLocalPort;
    DWORD dwRemoteAddr;
    DWORD dwRemotePort;
} MIB_TCPROW_LH;

// MIB_TCPTABLE  (x64 24 / x86 24 バイト)
typedef struct MIB_TCPTABLE {
    DWORD dwNumEntries;
    MIB_TCPROW_LH table[1];
} MIB_TCPTABLE;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MIB_TCPROW_LH
{
    public _Anonymous_e__Union Anonymous;
    public uint dwLocalAddr;
    public uint dwLocalPort;
    public uint dwRemoteAddr;
    public uint dwRemotePort;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MIB_TCPTABLE
{
    public uint dwNumEntries;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public MIB_TCPROW_LH[] table;
}
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure MIB_TCPROW_LH
    Public Anonymous As _Anonymous_e__Union
    Public dwLocalAddr As UInteger
    Public dwLocalPort As UInteger
    Public dwRemoteAddr As UInteger
    Public dwRemotePort As UInteger
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure MIB_TCPTABLE
    Public dwNumEntries As UInteger
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1)> Public table() As MIB_TCPROW_LH
End Structure
import ctypes
from ctypes import wintypes

class MIB_TCPROW_LH(ctypes.Structure):
    _fields_ = [
        ("Anonymous", _Anonymous_e__Union),
        ("dwLocalAddr", wintypes.DWORD),
        ("dwLocalPort", wintypes.DWORD),
        ("dwRemoteAddr", wintypes.DWORD),
        ("dwRemotePort", wintypes.DWORD),
    ]

class MIB_TCPTABLE(ctypes.Structure):
    _fields_ = [
        ("dwNumEntries", wintypes.DWORD),
        ("table", MIB_TCPROW_LH * 1),
    ]
#[repr(C)]
pub struct MIB_TCPROW_LH {
    pub Anonymous: _Anonymous_e__Union,
    pub dwLocalAddr: u32,
    pub dwLocalPort: u32,
    pub dwRemoteAddr: u32,
    pub dwRemotePort: u32,
}

#[repr(C)]
pub struct MIB_TCPTABLE {
    pub dwNumEntries: u32,
    pub table: [MIB_TCPROW_LH; 1],
}
import "golang.org/x/sys/windows"

type MIB_TCPROW_LH struct {
	Anonymous _Anonymous_e__Union
	dwLocalAddr uint32
	dwLocalPort uint32
	dwRemoteAddr uint32
	dwRemotePort uint32
}

type MIB_TCPTABLE struct {
	dwNumEntries uint32
	table [1]MIB_TCPROW_LH
}
type
  MIB_TCPROW_LH = record
    Anonymous: _Anonymous_e__Union;
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwRemoteAddr: DWORD;
    dwRemotePort: DWORD;
  end;

  MIB_TCPTABLE = record
    dwNumEntries: DWORD;
    table: array[0..0] of MIB_TCPROW_LH;
  end;
const MIB_TCPROW_LH = extern struct {
    Anonymous: _Anonymous_e__Union,
    dwLocalAddr: u32,
    dwLocalPort: u32,
    dwRemoteAddr: u32,
    dwRemotePort: u32,
};

const MIB_TCPTABLE = extern struct {
    dwNumEntries: u32,
    table: [1]MIB_TCPROW_LH,
};
type
  MIB_TCPROW_LH {.bycopy.} = object
    Anonymous: _Anonymous_e__Union
    dwLocalAddr: uint32
    dwLocalPort: uint32
    dwRemoteAddr: uint32
    dwRemotePort: uint32

  MIB_TCPTABLE {.bycopy.} = object
    dwNumEntries: uint32
    table: array[1, MIB_TCPROW_LH]
struct MIB_TCPROW_LH
{
    _Anonymous_e__Union Anonymous;
    uint dwLocalAddr;
    uint dwLocalPort;
    uint dwRemoteAddr;
    uint dwRemotePort;
}

struct MIB_TCPTABLE
{
    uint dwNumEntries;
    MIB_TCPROW_LH[1] table;
}

HSP用 定義

HSP3.7/3.8 は構造体機能が無いため4byte整数配列(dim)+peek/poke で操作(32/64bitでサイズ・位置が異なる場合はタブで分割)。IronHSP は NSTRUCT(#defstruct/stdim/->)で32/64bit共通。

; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x64 レイアウト)
; MIB_TCPTABLE サイズ: 24 バイト(x64)
dim st, 6    ; 4byte整数×6(構造体サイズ 24 / 4 切り上げ)
; dwNumEntries : DWORD (+0, 4byte)  st.0 = 値  /  値 = st.0   (lpoke/lpeek も可)
; table : MIB_TCPROW_LH (+4, 20byte)  varptr(st)+4 を基点に操作(20byte:入れ子/配列)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。
; IronHSP は NSTRUCT(構造体)をサポート。32bit/64bit どちらでも同じコードで動作します。
; ※GUID・入れ子構造体はデフォルト型でないため、依存する #defstruct を先に定義(下記に同梱)。
#defstruct global MIB_TCPROW_LH
    #field byte Anonymous 4
    #field int dwLocalAddr
    #field int dwLocalPort
    #field int dwRemoteAddr
    #field int dwRemotePort
#endstruct

#defstruct global MIB_TCPTABLE
    #field int dwNumEntries
    #field MIB_TCPROW_LH table 1
#endstruct

stdim st, MIB_TCPTABLE        ; NSTRUCT 変数を確保
st->dwNumEntries = 100
mes "dwNumEntries=" + st->dwNumEntries