在Windows 8上,我是否可以在C中使用任何标准功能,以获取处理系统中以太网的每个网络接口卡的IP地址和网络掩码列表?我能想到获取此类信息的另一种方法是解析ipconfig
的输出,但我真的不想在C ++中沿着这条路走下去。
答案 0 :(得分:1)
微软有一个名为The IP Helper API的东西。他们的文档非常详尽,Getting Started页面包含一些基本示例。
以下是GetAdaptersInfo的示例:
修改:在XP及更高版本中,不推荐使用GetAdaptersInfo()
,而是使用GetAdaptersAddresses()
(信用转到Remy Lebeau)。
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
printf("Adapter Name: %s\n", pAdapter->AdapterName);
printf("Adapter Desc: %s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (UINT 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("IP Address: %s\n", pAdapter->IpAddressList.IpAddress.String);
printf("IP Mask: %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\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
}
else
printf("\tDHCP Enabled: No\n");
pAdapter = pAdapter->Next;
}
以下是关于extracting network parameters的部分:
FIXED_INFO *pFixedInfo;
IP_ADDR_STRING *pIPAddr;
ULONG ulOutBufLen;
DWORD dwRetVal;
pFixedInfo = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
ulOutBufLen = sizeof (FIXED_INFO);
if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pFixedInfo);
pFixedInfo = (FIXED_INFO *) malloc(ulOutBufLen);
if (pFixedInfo == NULL) {
printf("Error allocating memory needed to call GetNetworkParams\n");
}
}
printf("\tHost Name: %s\n", pFixedInfo->HostName);
printf("\tDomain Name: %s\n", pFixedInfo->DomainName);
printf("\tDNS Servers:\n");
printf("\t\t%s\n", pFixedInfo->DnsServerList.IpAddress.String);
pIPAddr = pFixedInfo->DnsServerList.Next;
while (pIPAddr) {
printf("\t\t%s\n", pIPAddr->IpAddress.String);
pIPAddr = pIPAddr->Next;
}
printf("\tNode Type: ");
switch (pFixedInfo->NodeType) {
case 1:
printf("%s\n", "Broadcast");
break;
case 2:
printf("%s\n", "Peer to peer");
break;
case 4:
printf("%s\n", "Mixed");
break;
case 8:
printf("%s\n", "Hybrid");
break;
default:
printf("\n");
}
printf("\tNetBIOS Scope ID: %s\n", pFixedInfo->ScopeId);
if (pFixedInfo->EnableRouting)
printf("\tIP Routing Enabled: Yes\n");
else
printf("\tIP Routing Enabled: No\n");
if (pFixedInfo->EnableProxy)
printf("\tWINS Proxy Enabled: Yes\n");
else
printf("\tWINS Proxy Enabled: No\n");
if (pFixedInfo->EnableDns)
printf("\tNetBIOS Resolution Uses DNS: Yes\n");
else
printf("\tNetBIOS Resolution Uses DNS: No\n");
还有GetInterfaceInfo
等等。