栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

效果如下:

具体代码如下:

复制代码 代码如下:
#include
#include
#include

#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:%dn", pAdapterInfo->ComboIndex);
        printf("Adapter Name:%sn", pAdapterInfo->AdapterName);
        printf("Adapter Desc:%sn", 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:%dn", pAdapterInfo->Index);
        printf("Type:");
        switch (pAdapterInfo->Type)
        {
        case MIB_IF_TYPE_OTHER:printf("Othern"); break;
        case MIB_IF_TYPE_ETHERNET:printf("Ethernetn"); break;
        case MIB_IF_TYPE_TOKENRING:printf("Token Ringn"); break;
        case MIB_IF_TYPE_FDDI:printf("FDDIn"); break;
        case MIB_IF_TYPE_PPP:printf("PPPn"); break;
        case MIB_IF_TYPE_LOOPBACK:printf("Lookbackn"); break;
        case MIB_IF_TYPE_SLIP:printf("Slipn"); break;
        default:printf("Unknow type %ldn", pAdapterInfo->Type); break;
        }
        printf("IP Address:%sn", pAdapterInfo->IpAddressList.IpAddress.String);
        printf("IP Mask:%sn", pAdapterInfo->IpAddressList.IpMask.String);
        printf("Gateway:%sn", pAdapterInfo->GatewayList.IpAddress.String);

        if (pAdapterInfo->DhcpEnabled)
        {
            printf("DHCP Enabled:Yesn");
            printf("DHCP Server:%sn", 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:Non");
        }

        if (pAdapterInfo->HaveWins)
        {
            printf("Have Wins:Yesn");
            printf("Primary Wins Server:%sn", pAdapterInfo->PrimaryWinsServer.IpAddress.String);
            printf("Secondary Wins Server:%sn", pAdapterInfo->SecondaryWinsServer.IpAddress.String);
        }
        else
        {
            printf("Have Wins:Non");
        }

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

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

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

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号