獲取本地網(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)申請內(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;
}
欄 目:C語言
下一篇:c++遞歸實(shí)現(xiàn)n皇后問題代碼(八皇后問題)
本文標(biāo)題:獲取本地網(wǎng)卡適配器信息具體代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3852.html
您可能感興趣的文章
- 01-10Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼
- 01-10解析Linux下的時(shí)間函數(shù):設(shè)置以及獲取時(shí)間的方法
- 01-10DHCP:解析開發(fā)板上動(dòng)態(tài)獲取ip的2種實(shí)現(xiàn)方法詳解
- 01-10基于linux下獲取時(shí)間函數(shù)的詳解
- 01-10linux c 獲取本機(jī)公網(wǎng)IP的實(shí)現(xiàn)方法
- 01-10用c 獲取文件MD5值的實(shí)現(xiàn)方法
- 01-10使用C語言中的time函數(shù)獲取系統(tǒng)時(shí)間
- 01-10linux c程序中獲取shell腳本輸出的實(shí)現(xiàn)方法
- 01-10c++獲取進(jìn)程信息列表和進(jìn)程所調(diào)用的dll列表
- 01-10C++獲取任務(wù)欄打開程序窗口示例


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery