GetIfEntry2 函式 (netioapi.h)
GetIfEntry2 函式會擷取本機計算機上指定介面的資訊。
重要
對於驅動程式開發人員,建議您盡可能搭配MibIfEntryNormalWithoutStatistics使用 GetIfEntry2Ex ,以避免服務 NDIS OID 時發生死結。
語法
IPHLPAPI_DLL_LINKAGE _NETIOAPI_SUCCESS_ NETIOAPI_API GetIfEntry2(
PMIB_IF_ROW2 Row
);
參數
Row
在成功傳回時, MIB_IF_ROW2 結構的指標會接收本機計算機上的介面資訊。 在輸入時,MIB_IF_ROW2的 InterfaceLuid 或 InterfaceIndex 成員必須設定為要擷取資訊的介面。
傳回值
如果函式成功,傳回值會NO_ERROR。
如果函式失敗,傳回值就是下列其中一個錯誤碼。
傳回碼 | Description |
---|---|
|
系統找不到指定的檔案。 如果 Row 參數所指向 MIB_IF_ROW2之interfaceLuid 或 InterfaceIndex 成員所指定的網路介面 LUID 或介面索引不是本機電腦上的值,就會傳回此錯誤。 |
|
無效的參數已傳遞至 函式。 如果在 Row 參數中傳遞 NULL 參數,就會傳回此錯誤。 如果未指定 Row 參數所指向之MIB_IF_ROW2的 InterfaceLuid 和 InterfaceIndex 成員,也會傳回此錯誤。 |
|
使用 FormatMessage 函式來取得傳回錯誤的訊息字串。 |
備註
GetIfEntry2 函式是在 Windows Vista 和更新版本上定義。
在輸入上,必須在傳入 Row 參數的 MIB_IF_ROW2 結構中至少一個下列成員:InterfaceLuid 或 InterfaceIndex。
欄位會依照上面所列的順序使用。 因此,如果指定 InterfaceLuid ,則會使用此成員來判斷介面。 如果未為 InterfaceLuid 成員設定任何值, (這個成員的值設定為零) ,則 Next 會使用 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);
}
規格需求
需求 | 值 |
---|---|
最低支援的用戶端 | Windows Vista [傳統型應用程式 |UWP 應用程式] |
最低支援的伺服器 | Windows Server 2008 [傳統型應用程式 |UWP 應用程式] |
目標平台 | Windows |
標頭 | netioapi.h (包括 Iphlpapi.h) |
程式庫 | Iphlpapi.lib |
Dll | Iphlpapi.dll |
另請參閱
GetIfEntry