This article describes the C + + acquisition of the local Mac,ip,mask address method, shared for everyone to reference. The specific methods are as follows:
Copy Code code as follows:
#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;
To request memory for a structure
:: 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 represents the structure of 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 ();
}
I hope this article will help you with the C + + program design.