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

GetIcmpStatistics

関数
ICMPプロトコルの統計情報を取得する。
DLLIPHLPAPI.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD GetIcmpStatistics(
    MIB_ICMP* Statistics
);

パラメーター

名前方向説明
StatisticsMIB_ICMP*outローカルコンピューターの ICMP 統計情報を受け取る MIB_ICMP 構造体へのポインター。

戻り値の型: DWORD

公式ドキュメント

GetIcmpStatistics 関数は、ローカルコンピューターの IPv4 用 Internet Control Message Protocol (ICMP) 統計情報を取得します。

戻り値

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

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

戻り値 説明
ERROR_INVALID_PARAMETER
pStats パラメーターが NULL であるか、 GetIcmpStatisticspStats パラメーターで指されているメモリに書き込めません。
その他
返されたエラーに対応するメッセージ文字列を取得するには、 FormatMessage 関数を使用してください。

解説(Remarks)

GetIcmpStatistics 関数は、ローカルコンピューターの IPv4 用 ICMP 統計情報を返します。 Windows XP 以降では、GetIpStatisticsEx を使用して、ローカルコンピューターの IPv4 または IPv6 のいずれかの ICMP 統計情報を取得できます。

次の例では、ローカルコンピューターの IPv4 用 ICMP 統計情報を取得し、返されたデータから一部の情報を表示します。

#ifndef UNICODE
#define UNICODE
#endif

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

#pragma comment(lib, "iphlpapi.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int main()
{

    DWORD dwRetVal = 0;
    PMIB_ICMP pIcmpStats;

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

    dwRetVal = GetIcmpStatistics(pIcmpStats);
    if (dwRetVal == NO_ERROR) {
        wprintf(L"Number of incoming ICMP messages: %ld\n",
                pIcmpStats->stats.icmpInStats.dwMsgs);
        wprintf(L"Number of incoming ICMP errors received: %ld\n",
                pIcmpStats->stats.icmpInStats.dwErrors);
        wprintf(L"Number of outgoing ICMP messages: %ld\n",
                pIcmpStats->stats.icmpOutStats.dwMsgs);
        wprintf(L"Number of outgoing ICMP errors sent: %ld\n",
                pIcmpStats->stats.icmpOutStats.dwErrors);
    } else {
        wprintf(L"GetIcmpStatistics failed with error: %ld\n", dwRetVal);
    }

    if (pIcmpStats)
        FREE(pIcmpStats);

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

GetIcmpStatistics = ctypes.windll.iphlpapi.GetIcmpStatistics
GetIcmpStatistics.restype = wintypes.DWORD
GetIcmpStatistics.argtypes = [
    ctypes.c_void_p,  # Statistics : MIB_ICMP* out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	iphlpapi = windows.NewLazySystemDLL("IPHLPAPI.dll")
	procGetIcmpStatistics = iphlpapi.NewProc("GetIcmpStatistics")
)

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

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

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

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

let geticmpstatistics =
  foreign "GetIcmpStatistics"
    ((ptr void) @-> returning uint32_t)
(* Statistics : MIB_ICMP* 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 ("GetIcmpStatistics" get-icmp-statistics :convention :stdcall) :uint32
  (statistics :pointer))   ; MIB_ICMP* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetIcmpStatistics = Win32::API::More->new('IPHLPAPI',
    'DWORD GetIcmpStatistics(LPVOID Statistics)');
# my $ret = $GetIcmpStatistics->Call($Statistics);
# Statistics : MIB_ICMP* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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