C++獲取本機MAC,IP,MASK地址的方法
更新時間:2014年10月20日 11:25:06 投稿:shichen2014
這篇文章主要介紹了C++獲取本機MAC,IP,MASK地址的方法,主要通過IPHLPAPI.lib調(diào)用相關(guān)函數(shù)實現(xiàn)該功能,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了C++獲取本機MAC,IP,MASK地址的方法,分享給大家供大家參考。具體方法如下:
復(fù)制代碼 代碼如下:
#include "InitSock.h"
#include <stdio.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
u_char g_ucLocalMac[6];
DWORD g_dwGatewayIP;
DWORD g_dwLocalIP;
DWORD g_dwMask;
BOOL GetGlobalData()
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulSize = 0;
//為結(jié)構(gòu)申請內(nèi)存
::GetAdaptersInfo(pAdapterInfo, &ulSize);
pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulSize);
if ( ERROR_SUCCESS == ::GetAdaptersInfo(pAdapterInfo, &ulSize))
{
if (pAdapterInfo != NULL)
{
memcpy(g_ucLocalMac, pAdapterInfo->Address, 6);
g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
}
}
//in_addr表示IP的結(jié)構(gòu)
in_addr in;
in.S_un.S_addr = g_dwLocalIP;
printf(" IP Address:%-30s\n", ::inet_ntoa(in));
in.S_un.S_addr = g_dwGatewayIP;
printf(" Gateway Address:%-30s\n", ::inet_ntoa(in));
in.S_un.S_addr = g_dwMask;
printf(" MASK Address:%-30s\n", ::inet_ntoa(in));
u_char* p = g_ucLocalMac;
printf(" MAC:%02X-%02X-%02X-%02X-%02X-%02X\n", p[0], p[1], p[2], p[3], p[4], p[5]);
::GlobalFree(pAdapterInfo);
pAdapterInfo = NULL;
return TRUE;
}
void main()
{
CInitSock initSock;
GetGlobalData();
printf("*******************************");
getchar();
}
#include <stdio.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
u_char g_ucLocalMac[6];
DWORD g_dwGatewayIP;
DWORD g_dwLocalIP;
DWORD g_dwMask;
BOOL GetGlobalData()
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulSize = 0;
//為結(jié)構(gòu)申請內(nèi)存
::GetAdaptersInfo(pAdapterInfo, &ulSize);
pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulSize);
if ( ERROR_SUCCESS == ::GetAdaptersInfo(pAdapterInfo, &ulSize))
{
if (pAdapterInfo != NULL)
{
memcpy(g_ucLocalMac, pAdapterInfo->Address, 6);
g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
}
}
//in_addr表示IP的結(jié)構(gòu)
in_addr in;
in.S_un.S_addr = g_dwLocalIP;
printf(" IP Address:%-30s\n", ::inet_ntoa(in));
in.S_un.S_addr = g_dwGatewayIP;
printf(" Gateway Address:%-30s\n", ::inet_ntoa(in));
in.S_un.S_addr = g_dwMask;
printf(" MASK Address:%-30s\n", ::inet_ntoa(in));
u_char* p = g_ucLocalMac;
printf(" MAC:%02X-%02X-%02X-%02X-%02X-%02X\n", p[0], p[1], p[2], p[3], p[4], p[5]);
::GlobalFree(pAdapterInfo);
pAdapterInfo = NULL;
return TRUE;
}
void main()
{
CInitSock initSock;
GetGlobalData();
printf("*******************************");
getchar();
}
希望本文所述對大家的C++程序設(shè)計有所幫助。
相關(guān)文章
Qt音視頻開發(fā)之音頻播放QAudioOutput的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實現(xiàn)音頻播放QAudioOutput功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Qt開發(fā)有一定的幫助,需要的可以參考一下2023-03-03
基于Qt實現(xiàn)C/C++調(diào)用Matlab函數(shù)全過程
這篇文章給大家詳細(xì)介紹了基于Qt平臺實現(xiàn)C/C++調(diào)用Matlab函數(shù)全流程,文中通過圖文和代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
C++算法之海量數(shù)據(jù)處理方法的總結(jié)分析
本篇文章是對海量數(shù)據(jù)處理方法進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下2013-05-05
C++線性表深度解析之動態(tài)數(shù)組與單鏈表和棧及隊列的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)動態(tài)數(shù)組、單鏈表、棧、隊列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

