C++获取本机MAC,IP,MASK地址 C++获取本机MAC,IP,MASK地址的方法
人气:0想了解C++获取本机MAC,IP,MASK地址的方法的相关内容吗,在本文为您仔细讲解C++获取本机MAC,IP,MASK地址的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,获取,本机,MAC,IP,MASK,地址,下面大家一起来学习吧。
本文实例讲述了C++获取本机MAC,IP,MASK地址的方法,分享给大家供大家参考。具体方法如下:
复制代码 代码如下:
#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;
//为结构申请内存
::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的结构
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;
//为结构申请内存
::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的结构
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++程序设计有所帮助。
加载全部内容