[Network programming/C ++] reads Mac, IP, and other information of the local machine.

Source: Internet
Author: User

After a month of idleness, I asked my boss to create a PXE simulation program. The first step was to implement a DHCP client, which involved reading the MAC address and IP address of the local machine, I don't want to copy files to the public, so I will briefly describe how to use the getadapterinfo () function in the Windows SDK. This function is implemented in the header file iphlpapi. h.

Then there is the main implementation process, because the even environment is code: block10.5, here we will talk about the basic steps of Environment Configuration:

Choose Settings> compiler and debugger> linker settings, and add a lib file named libiphlpapi. A to link libraries in linker settings. (PS: here there is a small pattern found when codeblock is used, that is, the Lib file added in codeblock is generally a lib file before the file name in VC, and then the extension is, in addition to this, for example, the well-known ws2_32.lib file is libws2_32.a in codeblock)

After adding this file, you can write it.

 

The Code is as follows:

 

# Include <iostream> <br/> # include <string> <br/> # include <windows. h> <br/> # include <iphlpapi. h> <br/> # include <stdio. h> </P> <p> using namespace STD; </P> <p> void getmac () <br/>{< br/> ip_adapter_info * adapterinfo; // define the NIC <br/> adapterinfo = (ip_adapter_info *) malloc (sizeof (ip_adapter_info); // allocate space <br/> ulong dwbuflen = sizeof (ip_adapter_info ); // define the NIC size <br/> DWORD status = getadaptersinfo (adapterinfo, & DWB Uflen); // obtain the NIC Information </P> <p>/* <br/> the error message is generated when getadaptersinfo () is called for the first time. (error 111, it took me a long time ~~~ Failed) <br/> so we intentionally make a failed call to determine the size of the uloutbuflen variable (buffer), <br/> to buffer all data returned to padapterinfo. This method is a common programming mode for such structures and functions. <Br/> */</P> <p> If (status! = Error_success) // call failed for the first time <br/>{< br/> free (adapterinfo); <br/> adapterinfo = (ip_adapter_info *) malloc (dwbuflen ); // re-determine the space size <br/> Status = getadaptersinfo (adapterinfo, & dwbuflen); <br/>}< br/> If (status! = Error_success) <br/>{< br/> cout <"getadaptersinof failed with:" <status <Endl; <br/>}< br/> else <br/>{< br/> char Mac [18]; <br/> // sort out the MAC address format <br/> sprintf (MAC, "% 02x-% 02x-% 02x-% 02x-% 02x-% 02x ", adapterinfo-> Address [0], adapterinfo-> Address [1], adapterinfo-> Address [2], adapterinfo-> Address [3], adapterinfo-> Address [4], adapterinfo-> Address [5]); <br/> cout <"IP Address:" <adapterinfo-> ipaddresslist. IPaddress. string <Endl; <br/> cout <"MAC address:" <Mac <Endl; <br/>}</P> <p> If (adapterinfo) <br/> free (adapterinfo); </P> <p >}< br/>

 

 

 

TIPS:

 

Detailed description of ip_adapter_info struct:

 

Typedef struct _ ip_adapter_info {</P> <p> struct _ ip_adapter_info * Next; </P> <p> DWORD comboindex; </P> <p> char adaptername [max_adapter_name_length + 4]; </P> <p> char description [max_adapter_description_length + 4]; </P> <p> uint addresslength; </P> <p> byte Address [max_adapter_address_length]; </P> <p> DWORD index; </P> <p> uint type; </P> <p> uint dhcpenabled; </P> <p> pip_addr_string currentipaddress; </P> <p> ip_addr_string ipaddresslist; </P> <p> ip_addr_string gatewaylist; </P> <p> ip_addr_string dhcpserver; </P> <p> bool havewins; </P> <p> ip_addr_string primarywinsserver; </P> <p> ip_addr_string secondarywinsserver; </P> <p> time_t leaseobtained; </P> <p> time_t leaseexpires; </P> <p >}ip_adapter_info, * pip_adapter_info; </P> <p>

 

 

Running result:

 

 

 

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.