Gets the local network card adapter information specific code

  • 2020-04-02 02:03:32
  • OfStack

The effect is as follows:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/20131231163535293.jpg" >

The specific code is as follows:


#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;
    //Request memory for the adapter structure
    //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;
        }
    }
    //Gets the local adapter structure information
    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(" Local network card adapter information "));
    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;
}


Related articles: