GetIpStatistics 函式 (iphlpapi.h)
GetIpStatistics函式會擷取目前電腦的 IP 統計資料。
語法
IPHLPAPI_DLL_LINKAGE ULONG GetIpStatistics(
[out] PMIB_IPSTATS Statistics
);
參數
[out] Statistics
接收本機電腦的 IP 統計資料之 MIB_IPSTATS 結構的指標。
傳回值
如果函式成功,傳回值會NO_ERROR。
如果函式失敗,傳回值就是下列其中一個錯誤碼。
傳回碼 | 描述 |
---|---|
|
pStats參數為Null,或GetIpStatistics無法寫入pStats參數所指向的記憶體。 |
|
使用 FormatMessage 函式來取得傳回錯誤的訊息字串。 |
備註
GetIpStatistics 函式會傳回目前電腦上的 IPv4 統計資料。 在 Windows XP 和更新版本上, GetIpStatisticsEx 可用來取得 IPv4 或 IPv6 的 IP 統計資料。
範例
下列範例會擷取本機電腦的 IPv4 統計資料,並從傳回的資料列印值。
#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;
MIB_IPSTATS *pStats;
pStats = (MIB_IPSTATS *) MALLOC(sizeof (MIB_IPSTATS));
if (pStats == NULL) {
wprintf(L"Unable to allocate memory for MIB_IPSTATS\n");
exit(1);
}
dwRetval = GetIpStatistics(pStats);
if (dwRetval != NO_ERROR) {
wprintf(L"GetIpStatistics call failed with %d\n", dwRetval);
exit(1);
} else {
wprintf(L"IP forwarding: \t\t" );
switch (pStats->dwForwarding) {
case MIB_IP_FORWARDING:
wprintf(L"Enabled\n");
break;
case MIB_IP_NOT_FORWARDING:
wprintf(L"Disabled\n");
break;
default:
wprintf(L"unknown value = %d\n", pStats->dwForwarding);
break;
}
wprintf(L"Default initial TTL: \t\t\t\t\t%u\n", pStats->dwDefaultTTL);
wprintf(L"Number of received datagrams: \t\t\t\t%u\n", pStats->dwInReceives);
wprintf(L"Number of received datagrams with header errors: \t%u\n", pStats->dwInHdrErrors);
wprintf(L"Number of received datagrams with address errors: \t%u\n", pStats->dwInAddrErrors);
wprintf(L"Number of datagrams forwarded: \t\t\t\t%ld\n", pStats->dwForwDatagrams);
wprintf(L"Number of received datagrams with an unknown protocol: \t%u\n", pStats->dwInUnknownProtos);
wprintf(L"Number of received datagrams discarded: \t\t%u\n", pStats->dwInDiscards);
wprintf(L"Number of received datagrams delivered: \t\t%u\n", pStats->dwInDelivers);
wprintf(L"Number of outgoing datagrams requested to transmit: \t%u\n", pStats->dwOutRequests);
wprintf(L"Number of outgoing datagrams discarded for routing: \t%u\n", pStats->dwRoutingDiscards);
wprintf(L"Number of outgoing datagrams discarded: \t\t%u\n", pStats->dwOutDiscards);
wprintf(L"Number of outgoing datagrams with no route to destination discarded: %u\n", pStats->dwOutNoRoutes);
wprintf(L"Fragment reassembly timeout: \t\t\t\t%u\n", pStats->dwReasmTimeout);
wprintf(L"Number of datagrams that required reassembly: \t\t%u\n", pStats->dwReasmReqds);
wprintf(L"Number of datagrams successfully reassembled: \t\t%u\n", pStats->dwReasmOks);
wprintf(L"Number of datagrams that could not be reassembled: \t%u\n", pStats->dwReasmFails);
wprintf(L"Number of datagrams fragmented successfully: \t\t%u\n", pStats->dwFragOks);
wprintf(L"Number of datagrams not fragmented and discarded: \t%u\n", pStats->dwFragFails);
wprintf(L"Number of fragments created: \t\t\t\t%u\n", pStats->dwFragCreates);
wprintf(L"Number of interfaces: \t\t\t\t\t%u\n", pStats->dwNumIf);
wprintf(L"Number of IP addresses: \t\t\t\t%u\n", pStats->dwNumAddr);
wprintf(L"Number of routes: \t\t\t\t\t%u\n", pStats->dwNumRoutes);
}
// Free memory allocated for the MIB_IPSTATS structure
if (pStats)
FREE(pStats);
return 0;
}
需求
最低支援的用戶端 | Windows 2000 專業版 [僅限傳統型應用程式] |
最低支援的伺服器 | Windows 2000 Server [僅限傳統型應用程式] |
目標平台 | Windows |
標頭 | iphlpapi.h |
程式庫 | Iphlpapi.lib |
Dll | Iphlpapi.dll |