獲取本地網(wǎng)卡適配器信息具體代碼
效果如下:
具體代碼如下:
#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;
//為適配器結(jié)構(gòu)申請(qǐng)內(nèi)存
//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;
}
}
//取得本地適配器結(jié)構(gòu)信息
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("本地網(wǎng)卡適配器信息"));
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;
}
相關(guān)文章
C語(yǔ)言完整實(shí)現(xiàn)12種排序算法(小結(jié))
本文主要介紹了C語(yǔ)言完整實(shí)現(xiàn)12種排序算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C語(yǔ)言程序中結(jié)構(gòu)體的內(nèi)存對(duì)齊詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言程序中結(jié)構(gòu)體的內(nèi)存對(duì)齊的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2022-11-11C語(yǔ)言算法練習(xí)之打魚(yú)還是曬網(wǎng)
這篇文章主要該大家分享C語(yǔ)言打魚(yú)還是曬網(wǎng)的練習(xí),文章主要通過(guò)三天打魚(yú)兩天曬網(wǎng)的俗語(yǔ)提出問(wèn)題,在某一天輪到打魚(yú)還是曬網(wǎng),下面來(lái)看詳細(xì)內(nèi)容吧,需要的朋友可以參考一下2022-03-03使用C++實(shí)現(xiàn)工資管理中的隨機(jī)教師信息生成功能
這篇文章主要介紹了使用C++實(shí)現(xiàn)工資管理中的隨機(jī)教師信息生成功能,想要做一個(gè)教師工資管理系統(tǒng),就必須得準(zhǔn)備好數(shù)據(jù),但是這些數(shù)據(jù)如果用手一行一行地敲,那么工作量是非常大的,因此,我就產(chǎn)生了用C語(yǔ)言實(shí)現(xiàn)直接生成大量的教師基本信息的想法,需要的朋友可以參考下2023-05-05關(guān)于C++出現(xiàn)Bus error問(wèn)題的排查與解決
項(xiàng)目代碼中經(jīng)常出現(xiàn)莫名其妙的Bus error問(wèn)題,并且代碼中增加很多try catch 后依然不能將錯(cuò)誤捕獲,一旦Bus erro出現(xiàn),進(jìn)程直接崩潰掉,所以本文給大家介紹了關(guān)于C++出現(xiàn)Bus error問(wèn)題的排查與解決,需要的朋友可以參考下2024-01-01基于C++實(shí)現(xiàn)去除字符串頭尾指定字符功能
編程時(shí)我們經(jīng)常需要對(duì)字符串進(jìn)行操作,其中有一項(xiàng)操作就是去除字符串的頭(尾)指定的字符,比如空格。本文為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)這一效果,需要的可以參考一下2022-04-04