C + + method to obtain native Mac,ip,mask address _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.