c实现获取本地网卡适配器信息的代码

发布时间:2020-11-21编辑:脚本学堂
分享一例c语言实现的获取本地网卡适配器信息的代码,供大家学习参考。

本节:
获取本地网卡适配器信息的c代码。

效果,如下图:
获取本地网卡适配器信息

完整代码:
 

复制代码 代码示例:
#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:%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;
}