获取本地网卡适配器信息具体代码

 更新时间:2013年12月31日 16:35:00   作者:  
这篇文章主要介绍了获取本地网卡适配器信息具体代码,有需要的朋友可以参考一下

效果如下:

具体代码如下:

复制代码 代码如下:

#include <Windows.h>
#include <IPHlpApi.h>
#include <stdio.h>

#pragma comment(lib, "IPHlpApi")
#pragma comment(lib, "ws2_32")

int main(int argc, char **argv)
{
    PIP_ADAPTER_INFO pAdapterInfo = NULL;
    ULONG ulLen = sizeof(IP_ADAPTER_INFO);
    struct tm newtime;
    char szBuffer[32];
    errno_t error;

    //为适配器结构申请内存
    //pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, ulLen);
    pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
    if (NULL == pAdapterInfo)
    {
        printf("Error allocating memory needed to call GetAdaptersInfo.\n");
        return 1;
    }

    if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pAdapterInfo, &ulLen))
    {
        HeapFree(GetProcessHeap(), 0, pAdapterInfo);
        pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, ulLen);
        if (NULL == pAdapterInfo)
        {
            printf("Error allocating memory needed to call GetAdaptersInfo.\n");
            return 1;
        }
    }

    //取得本地适配器结构信息
    if (ERROR_SUCCESS != GetAdaptersInfo(pAdapterInfo, &ulLen))
    {
        printf("GetAdaptersInfo error!\n");
        return 0;
    }
    if (NULL == pAdapterInfo)
    {
        printf("There is no adapters!\n");
        return 0;
    }

    SetConsoleTitle(TEXT("本地网卡适配器信息"));

    do
    {
        printf("ComboIndex:%d\n", pAdapterInfo->ComboIndex);
        printf("Adapter Name:%s\n", pAdapterInfo->AdapterName);
        printf("Adapter Desc:%s\n", pAdapterInfo->Description);
        printf("Adapter Addr:");
        for (size_t i = 0; i < pAdapterInfo->AddressLength; i++)
        {
            if (i == (pAdapterInfo->AddressLength - 1))
            {
                printf("%02X", (int)pAdapterInfo->Address[i]);
            }
            else
            {
                printf("%02X-", (int)pAdapterInfo->Address[i]);
            }
        }
        printf("\n");
        printf("Index:%d\n", pAdapterInfo->Index);
        printf("Type:");
        switch (pAdapterInfo->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("Lookback\n"); break;
        case MIB_IF_TYPE_SLIP:printf("Slip\n"); break;
        default:printf("Unknow type %ld\n", pAdapterInfo->Type); break;
        }
        printf("IP Address:%s\n", pAdapterInfo->IpAddressList.IpAddress.String);
        printf("IP Mask:%s\n", pAdapterInfo->IpAddressList.IpMask.String);
        printf("Gateway:%s\n", pAdapterInfo->GatewayList.IpAddress.String);

        if (pAdapterInfo->DhcpEnabled)
        {
            printf("DHCP Enabled:Yes\n");
            printf("DHCP Server:%s\n", pAdapterInfo->DhcpServer.IpAddress.String);
            printf("Lease Obtained:");
            error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseObtained);
            if (error)
            {
                printf("Invalid Argument to _localtime32_s.\n");
            }
            else
            {
                error = asctime_s(szBuffer, 32, &newtime);
                if (error)
                {
                    printf("Invalid Argument to asctime_s.\n");
                }
                else
                {
                    printf("%s", szBuffer);
                }
            }

            printf("Lease Expires:");
            error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseExpires);
            if (error)
            {
                printf("Invalid Argument to _localtime32_s.\n");
            }
            else
            {
                error = asctime_s(szBuffer, 32, &newtime);
                if (error)
                {
                    printf("Invalid Argument to asctime_s.\n");
                }
                else
                {
                    printf("%s", szBuffer);
                }
            }
        }
        else
        {
            printf("DHCP Enabled:No\n");
        }

        if (pAdapterInfo->HaveWins)
        {
            printf("Have Wins:Yes\n");
            printf("Primary Wins Server:%s\n", pAdapterInfo->PrimaryWinsServer.IpAddress.String);
            printf("Secondary Wins Server:%s\n", pAdapterInfo->SecondaryWinsServer.IpAddress.String);
        }
        else
        {
            printf("Have Wins:No\n");
        }

        printf("=================================================================\n");

        pAdapterInfo = pAdapterInfo->Next;
    } while (pAdapterInfo);

    if (pAdapterInfo)
    {
        HeapFree(GetProcessHeap(), 0, pAdapterInfo);
    }

    return 0;
}

相关文章

  • 将字符串str1复制为字符串str2的三种解决方法

    将字符串str1复制为字符串str2的三种解决方法

    以下是对将字符串str1复制为字符串str2的三种解决方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-10-10
  • C++常用的#include头文件总结

    C++常用的#include头文件总结

    这篇文章主要介绍了C++常用的#include头文件,对初学者理解C++程序设计大有好处的相关资料
    2014-07-07
  • c++ #include是怎么样工作的?

    c++ #include是怎么样工作的?

    大多数园友可能对“#include”比较熟悉,因为我们写C/C++程序的时候都会写的字符串之一,但是它是具体怎么工作的?或者它的原理是什么呢?
    2013-01-01
  • Cocos2d-x中使用CCScrollView来实现关卡选择实例

    Cocos2d-x中使用CCScrollView来实现关卡选择实例

    这篇文章主要介绍了Cocos2d-x中使用CCScrollView来实现关卡的选择实例,本文在代码中用大量注释讲解了CCScrollView的使用,需要的朋友可以参考下
    2014-09-09
  • C++实现推箱子小游戏源码

    C++实现推箱子小游戏源码

    这篇文章主要为大家详细介绍了C++实现推箱子小游戏源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • C及C++中typedef的简单使用介绍

    C及C++中typedef的简单使用介绍

    C/C++中关键字typedef的理解不是多透彻,今天小编抽空给大家分享下C及C++中typedef的简单使用介绍,需要的朋友可以参考下
    2016-10-10
  • Qt编写地图迁徙图的实现示例

    Qt编写地图迁徙图的实现示例

    本文主要介绍了Qt编写地图迁徙图的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 详解C++循环创建多级目录及判断目录是否存在的方法

    详解C++循环创建多级目录及判断目录是否存在的方法

    这篇文章主要介绍了C++循环创建多级目录及判断目录是否存在的方法,文中代码有一个针对各种系统进行判断来加载不同头文件的方法,需要的朋友可以参考下
    2016-03-03
  • C++ Boost.Signals2信号/槽概念

    C++ Boost.Signals2信号/槽概念

    Boost是为C++语言标准库提供扩展的一些C++程序库的总称。Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一,是为C++语言标准库提供扩展的一些C++程序库的总称
    2022-12-12
  • 深入解析C++中的构造函数和析构函数

    深入解析C++中的构造函数和析构函数

    析构函数:在撤销对象占用的内存之前,进行一些操作的函数。析构函数不能被重载,只能有一个
    2013-09-09

最新评论