Use getadaptersinfo in C ++ to obtain Nic configuration and IP address information

Source: Internet
Author: User

One machine may have more than one Nic, but each Nic has only one MAC address, and each Nic may have multiple IP addresses. For example, in a normal laptop, there will be two types of wireless Nic and wired NIC (Network Interface). Therefore, if you want to obtain the IP and MAC address information of all NICs of the local machine, you must obtain each Nic in sequence, in the Windows SDK, use the ip_adapter_info structure to store the NIC information, including the NIC name, Nic description, Nic MAC address, and nic IP address, the structure is described as follows:

Typedef struct _ ip_adapter_info {struct _ ip_adapter_info * Next; // pointer to the next adapter information in the linked list DWORD comboindex; // reserved value char adaptername [max_adapter_name_length + 4]; // The adapter name char description [max_adapter_description_length + 4] using the ANSI string; // The adapter description uint addresslength using the ANSI string; // The length of the adapter hardware address in bytes byte Address [max_adapter_address_length]; // The hardware address is represented by the DWORD index in the byte array; // The adapter index uint type; // The adapter type, there are mainly the following types:/** mib_if_type_other 1 * mib_if_type_ethernet 6 * Listen 9 * mib_if_type_fddi 15 * mib_if_type_ppp 23*24 * mib_if_type_slip 28 */uint dhcpenabled; // specify whether the DHCP pip_addr_string currentipaddress is enabled for this adapter; // The reserved value ip_addr_string ipaddresslist; // The IPv4 address linked list ip_addr_string gatewaylist of this adapter; // The Gateway IPv4 address linked list of the adapter ip_addr_string dhcpserver; // IPv4 address linked list of the DHCP server of the adapter bool havewins; Listen primarywinsserver; then secondarywinsserver; time_t expired; time_t leaseexpires ;} ip_adapter_info, * pip_adapter_info;

Because there may be multiple NICs, the struct _ ip_adapter_info * next field is a linked list structure pointer. Because one Nic may have multiple IP addresses, the ip_addr_string field should also be a linked list structure, the information is as follows:

Typedef struct _ ip_addr_string {struct _ ip_addr_string * Next; // point to the same type of node, that is, the next IP address (if there are multiple IP addresses) ip_address_string IPaddress; // ip_mask_string ipmask; // IP subnet mask DWORD context; // network table entry. This value corresponds to the ntecontext parameter} ip_addr_string in addipaddredd and deleteipaddress functions;

After learning the above information, you can call the getadaptersinfo function to obtain information about the NIC. The common code is as follows:

# Include <winsock2.h> # include <lplpapi. h> # include <iostream> using namespace STD; # pragma comment (Lib, "iphlpapi. lib ") // you need to add the lplpapi. lib library int main (INT argc, char * argv []) {// pip_adapter_info structure pointer stores the information of the local Nic pip_adapter_info pipadapterinfo = new ip_adapter_info (); // obtain the struct size, used for getadaptersinfo parameter unsigned long stsize = sizeof (ip_adapter_info); // call the getadaptersinfo function to fill in the pipadapterinfo pointer variable. The stsize parameter is both an input and an output int NREL = getadaptersinfo, & stsize); // record the number of NICs int netcardnum = 0; // record the number of IP addresses on each Nic int ipnumpernetcard = 0; If (error_buffer_overflow = NREL) {// if the function returns error_buffer_overflow //, it indicates that the memory passed by the getadaptersinfo parameter is insufficient, and stsize is sent out, the size of the required space. // This also shows why stsize is both an input and an output. // release the original memory space Delete pipadapterinfo; // re-apply for memory space to store all network card information pipadapterinfo = (pip_adapter_info) New byte [stsize]; // call the getadaptersinfo function again to fill the pipadapterinfo pointer variable NREL = getadaptersinfo (pipadapterinfo, & stsize) ;}if (error_success = NREL) {// output Nic information // multiple NICs may exist. Therefore, while (pipadapterinfo) can be determined cyclically) {cout <"Number of NICs:" <++ netcardnum <Endl; cout <"network adapter name:" <pipadapterinfo-> adaptername <Endl; cout <"Nic Description:" <pipadapterinfo-> description <Endl; Switch (pipadapterinfo-> type) {Case mib_if_type_other: cout <"Nic type: "<" other "<Endl; break; Case mib_if_type_ethernet: cout <" Nic type: "<" Ethernet "<Endl; break; Case mib_if_type_tokenring: cout <"Nic type:" <"tokenring" <Endl; break; Case mib_if_type_fddi: cout <"Nic type:" <"FDDI" <Endl; break; Case mib_if_type_ppp: printf ("PP \ n"); cout <"Nic type:" <"PPP" <Endl; break; Case mib_if_type_loopback: cout <"Nic type:" <"loopback" <Endl; break; Case mib_if_type_slip: cout <"Nic type:" <"slip" <Endl; break; default: break;} cout <"Nic MAC address:"; for (DWORD I = 0; I <pipadapterinfo-> addresslength; I ++) if (I <pipadapterinfo-> AddressLength-1) {printf ("% 02x-", pipadapterinfo-> Address [I]);} else {printf ("% 02x \ n ", pipadapterinfo-> Address [I]);} cout <"Nic IP Address:" <Endl; // The NIC may have multiple IP addresses, therefore, the ip_addr_string * pipaddrstring = & (pipadapterinfo-> ipaddresslist); do {cout <"number of IP addresses on the NIC:" <++ ipnumpernetcard <Endl; cout <"IP Address:" <pipaddrstring-> IPaddress. string <Endl; cout <"subnet address:" <pipaddrstring-> ipmask. string <Endl; cout <"gateway address:" <pipadapterinfo-> gatewaylist. IPaddress. string <Endl; pipaddrstring = pipaddrstring-> next;} while (pipaddrstring); pipadapterinfo = pipadapterinfo-> next; cout <"condition" <Endl ;}} // release the memory space if (pipadapterinfo) {Delete pipadapterinfo;} return 0 ;}

Execution 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.