C++ 中的网络适配器信息 [英] Network Adapter Information in C++

查看:80
本文介绍了C++ 中的网络适配器信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 8 上,我可以在 C 中使用任何标准函数来获取系统中处理以太网的每个网络接口卡的 IP 地址和网络掩码列表吗?我能想到的获取此类信息的唯一另一种方法是解析 ipconfig 的输出,但我真的不想在 C++ 中走这条路.

On Windows 8, is there any standard function I can use in C in order to get a list of IP addresses and netmasks of each Network Interface Card that handles Ethernet in the system? The only other way I can think of getting such information is by parsing the output of ipconfig, but I really don't want to go down that route in C++.

推荐答案

Microsoft 有一种叫做 IP 助手 API.他们的文档非常详尽,入门 页面包含一些基本示例.

Microsoft has something called The IP Helper API. Their documentation is very thorough, and the Getting Started page contains a few basic examples.

以下是 GetAdaptersInfo 的示例:

编辑:在 XP 及更高版本上,GetAdaptersInfo() 已弃用,请使用 GetAdaptersAddresses()(归功于 Remy Lebeau).

Edit: On XP and later, GetAdaptersInfo() is deprecated, use GetAdaptersAddresses() instead (credit goes to 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;
}

这是关于提取网络参数:

    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 等等.

这篇关于C++ 中的网络适配器信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆