国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

獲取本地網卡適配器信息具體代碼

2020-01-26 15:38:55
字體:
來源:轉載
供稿:網友

效果如下:

具體代碼如下:

復制代碼 代碼如下:

#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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 常熟市| 新竹县| 桂平市| 长海县| 宜春市| 会理县| 清镇市| 桦川县| 连云港市| 新巴尔虎右旗| 广南县| 德钦县| 绥中县| 遂昌县| 句容市| 葫芦岛市| 扎鲁特旗| 安达市| 天全县| 九龙县| 平和县| 临江市| 日土县| 湖州市| 湘潭县| 长白| 通江县| 重庆市| 普兰店市| 寿宁县| 溧阳市| 女性| 育儿| 建昌县| 中宁县| 旌德县| 岳池县| 乐东| 通河县| 临沭县| 江西省|