C++獲得本機MAC,IP,MASK地址的辦法。本站提示廣大學習愛好者:(C++獲得本機MAC,IP,MASK地址的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
}
願望本文所述對年夜家的C++法式設計有所贊助。