getAdaptersInfo 函数 (iphlpapi.h)

您所在的位置:网站首页 dnsserversearchorder失败 getAdaptersInfo 函数 (iphlpapi.h)

getAdaptersInfo 函数 (iphlpapi.h)

2024-07-10 12:45| 来源: 网络整理| 查看: 265

getAdaptersInfo 函数 (iphlpapi.h) 项目08/24/2023

GetAdaptersInfo 函数检索本地计算机的适配器信息。

在 Windows XP 及更高版本上: 使用 GetAdaptersAddresses 函数而不是 GetAdaptersInfo

语法 IPHLPAPI_DLL_LINKAGE ULONG GetAdaptersInfo( [out] PIP_ADAPTER_INFO AdapterInfo, [in, out] PULONG SizePointer ); parameters

[out] AdapterInfo

指向接收IP_ADAPTER_INFO结构链接列表的缓冲区 的 指针。

[in, out] SizePointer

指向 ULONG 变量的指针,该变量指定 pAdapterInfo 参数指向的缓冲区的大小。 如果此大小不足以保存适配器信息, 则 GetAdaptersInfo 会用所需的大小填充此变量,并返回 ERROR_BUFFER_OVERFLOW错误代码。

返回值

如果函数成功,则返回值 ERROR_SUCCESS (定义为与 NO_ERROR) 相同的值。

如果函数失败,则返回值为以下错误代码之一。

返回代码 说明 ERROR_BUFFER_OVERFLOW 用于接收适配器信息的缓冲区太小。 如果 pOutBufLen 参数指示的缓冲区大小太小而无法保存适配器信息,或者 pAdapterInfo 参数为 NULL 指针,则返回此值。 返回此错误代码时, pOutBufLen 参数指向所需的缓冲区大小。 ERROR_INVALID_DATA 检索了无效的适配器信息。 ERROR_INVALID_PARAMETER 其中一个参数无效。 如果 pOutBufLen 参数为 NULL 指针,或者调用进程对 pOutBufLen 所指向的内存没有读/写访问权限,或者调用进程对 pAdapterInfo 参数所指向的内存没有写入访问权限,则返回此错误。 ERROR_NO_DATA 本地计算机不存在适配器信息。 ERROR_NOT_SUPPORTED 在本地计算机上运行的操作系统不支持 GetAdaptersInfo 函数。 其他 如果函数失败,请使用 FormatMessage 获取返回错误的消息字符串。 注解

GetAdaptersInfo 函数只能检索 IPv4 地址的信息。

在 Windows 10 之前的版本中,可从“网络连接”文件夹控制适配器在此函数返回的列表中出现的顺序:从“高级”菜单中选择“高级设置”菜单项。 从 Windows 10 开始,顺序未指定。

GetAdaptersInfo 和 GetInterfaceInfo 函数不返回有关 IPv4 环回接口的信息。 有关环回接口的信息由 GetIpAddrTable 函数返回。

在 Windows XP 及更高版本上: GetAdaptersInfo 返回的适配器列表包括单向适配器。 若要生成可发送和接收数据的适配器列表,请调用 GetUniDirectionalAdapterInfo,并从 GetAdaptersInfo 返回的列表中排除返回的适配器。

示例

此示例检索适配器信息并打印每个适配器的各种属性。

#include #include #include #include #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 __cdecl main() { /* Declare and initialize variables */ // It is possible for an adapter to have multiple // IPv4 addresses, gateways, and secondary WINS servers // assigned to the adapter. // // Note that this sample code only prints out the // first entry for the IP address/mask, and gateway, and // the primary and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; UINT i; /* variables used to print DHCP time info */ struct tm newtime; char buffer[32]; errno_t error; ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO)); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return 1; } // Make an initial call to GetAdaptersInfo to get // the necessary size into the ulOutBufLen variable if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { FREE(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return 1; } } if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { pAdapter = pAdapterInfo; while (pAdapter) { printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex); printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName); printf("\tAdapter Desc: \t%s\n", pAdapter->Description); printf("\tAdapter Addr: \t"); for (i = 0; i < pAdapter->AddressLength; i++) { if (i == (pAdapter->AddressLength - 1)) printf("%.2X\n", (int) pAdapter->Address[i]); else printf("%.2X-", (int) pAdapter->Address[i]); } printf("\tIndex: \t%d\n", pAdapter->Index); printf("\tType: \t"); switch (pAdapter->Type) { case MIB_IF_TYPE_OTHER: printf("Other\n"); break; case MIB_IF_TYPE_ETHERNET: printf("Ethernet\n"); break; case MIB_IF_TYPE_TOKENRING: printf("Token Ring\n"); break; case MIB_IF_TYPE_FDDI: printf("FDDI\n"); break; case MIB_IF_TYPE_PPP: printf("PPP\n"); break; case MIB_IF_TYPE_LOOPBACK: printf("Loopback\n"); break; case MIB_IF_TYPE_SLIP: printf("Slip\n"); break; default: printf("Unknown type %ld\n", pAdapter->Type); break; } printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String); printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String); printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String); printf("\t***\n"); if (pAdapter->DhcpEnabled) { printf("\tDHCP Enabled: Yes\n"); printf("\t DHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String); printf("\t Lease Obtained: "); /* Display local time */ error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ printf("%s", buffer); } printf("\t Lease Expires: "); error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseExpires); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ printf("%s", buffer); } } else printf("\tDHCP Enabled: No\n"); if (pAdapter->HaveWins) { printf("\tHave Wins: Yes\n"); printf("\t Primary Wins Server: %s\n", pAdapter->PrimaryWinsServer.IpAddress.String); printf("\t Secondary Wins Server: %s\n", pAdapter->SecondaryWinsServer.IpAddress.String); } else printf("\tHave Wins: No\n"); pAdapter = pAdapter->Next; printf("\n"); } } else { printf("GetAdaptersInfo failed with error: %d\n", dwRetVal); } if (pAdapterInfo) FREE(pAdapterInfo); return 0; } 要求     最低受支持的客户端 Windows 2000 Professional [仅限桌面应用] 最低受支持的服务器 Windows 2000 Server [仅限桌面应用] 目标平台 Windows 标头 iphlpapi.h Library Iphlpapi.lib DLL Iphlpapi.dll 另请参阅

GetAdaptersAddresses

GetInterfaceInfo

GetIpAddrTable

GetNumberOfInterfaces

GetUniDirectionalAdapterInfo

IP 帮助程序函数参考

IP 帮助程序起始页

IP_ADAPTER_INFO



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3