C + + Obtain the IP and MAC address information of all network cards in this machine _c language

Source: Internet
Author: User
Tags ip number reserved

There may not be only one NIC on a machine, but each NIC has only one MAC address, and each network card may be configured with multiple IP addresses; As in the ordinary notebook computer, there will be wireless network card and wired network card (network interface) Two; therefore, if you want to obtain all the network card IP and MAC address information, You must obtain each network card sequentially, then obtain its information, etc. in the Windows SDK, the IP_ADAPTER_INFO structure is used to store the NIC information, including the network card name, the network card description, the MAC address of the network card, the network card IP, etc., the main description of the structure is as follows:

typedef struct _IP_ADAPTER_INFO {struct _ip_adapter_info* next;//pointer to the next adapter information in the list DWORD comboindex;//reserved value char Adap Tername[max_adapter_name_length + 4];//Adapter name using ANSI string, char Description[max_adapter_description_length + 4];// Adapter description using ANSI string representation UINT addresslength;//Adapter hardware address in bytes length byte address[max_adapter_address_length];//hardware address represented in byte array DW ORD index;//Adapter Index UINT type;//Adapter type, mainly the following: * * Mib_if_type_other 1 * mib_if_type_ethernet 6 * mib_i F_type_tokenring 9 * mib_if_type_fddi * MIB_IF_TYPE_PPP * mib_if_type_loopback * mib_if_t Ype_slip/UINT dhcpenabled;//Specifies whether this adapter will open DHCP pip_addr_string currentipaddress;//reserved value ip_addr_string sslist;//the adapter's IPV4 address List ip_addr_string gatewaylist;//The adapter's Gateway IPV4 address List ip_addr_string dhcpserver;//The adapter's DHCP server
  Address List BOOL Havewins;
  Ip_addr_string Primarywinsserver;
  Ip_addr_string Secondarywinsserver;
  time_t leaseobtained;
  time_t leaseexpires; } Ip_adapter_iNfo,*pip_adapter_info; 

Because there may be more than one NIC, the struct _ip_adapter_info* next field is a linked list structure pointer, because a network card may have multiple IPs, so the Ip_addr_string field should also be a list structure with the following information:

typedef struct _IP_ADDR_STRING
{
  struct _ip_addr_string* next;//point to the same type of node, that is, the next IP (if there is more IP)
  ip_address_ STRING IPAddress; IP address information
  ip_mask_string ipmask//ip subnet mask
  DWORD context;//Network table entry. This value corresponds to the Ntecontext parameter in the Addipaddredd and deleteipaddress functions
} ip_addr_string;

To sum up, use the following figure to describe the structure of the network card storage information, perhaps more clear:

After basic knowledge of the above information, you can call the GetAdaptersInfo function to get the relevant network card information, its common code is as follows:

#include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> using namespace std; #pragma comment (lib, "Iphlpapi.lib")//need to add Iphlpapi.lib library int main (int argc, char* argv[]) {//pip_adapter_info structure body pointer storage native Network
  Card information Pip_adapter_info pipadapterinfo = new Ip_adapter_info ();
  The structure body size is obtained and used for getadaptersinfo parameter unsigned long stsize = sizeof (Ip_adapter_info); Call the GetAdaptersInfo function and populate the Pipadapterinfo pointer variable, where the Stsize parameter is both an input and an output int nRel = GetAdaptersInfo (Pipadapterinfo,
  &stsize);
  Record the number of network cards int netcardnum = 0;
  Record the number of IP addresses on each net card int ipnumpernetcard = 0; if (Error_buffer_overflow = = NRel) {//If the function returns Error_buffer_overflow//It indicates that the GetAdaptersInfo parameter is not passing enough memory space and that it is outgoing stsize
    , indicating the amount of space required//This is also why Stsize is both an input and an output//release of the original memory space delete pipadapterinfo;
    Re-apply memory space to store all network card information Pipadapterinfo = (pip_adapter_info) new byte[stsize];  
  Call the GetAdaptersInfo function again, filling the pipadapterinfo pointer variable nrel=getadaptersinfo (pipadapterinfo,&stsize); } if (error_succesS = = NRel) {//Output network card information//may have more than one NIC, so through the loop to determine while (Pipadapterinfo) {cout<< "Number of network cards:" <<++netcardn
    um<<endl;
    cout<< "Network card name:" <<pIpAdapterInfo->AdapterName<<endl;
    cout<< "Network card description:" <<pIpAdapterInfo->Description<<endl;
      Switch (pipadapterinfo->type) {case mib_if_type_other:cout<< "Nic type:" << "Other" <<endl;
    Break
      Case mib_if_type_ethernet:cout<< "Network adapter Type:" << "ETHERNET" <<endl;
    Break
      Case mib_if_type_tokenring:cout<< "Network adapter Type:" << "Tokenring" <<endl;
    Break
      Case mib_if_type_fddi:cout<< "Nic type:" << "FDDI" <<endl;
    Break
      Case mib_if_type_ppp:printf ("pp\n");
      cout<< "Network Adapter Type:" << "PPP" <<endl;
    Break
      Case mib_if_type_loopback:cout<< "Network adapter Type:" << "loopback" <<endl;
    Break Case mib_if_type_slip:cout<< "Network adapter 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<< "network card IP address is as follows:" <<endl;
      may have multiple IP card, so through the cycle to Judge Ip_addr_string *pipaddrstring =& (pipadapterinfo->ipaddresslist);
        Do {cout<< "IP number on this network card:" <<++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<< "--------------------------------------------------------------------" <<endl;
  }//Free memory space if (pipadapterinfo) {delete pipadapterinfo;
return 0; }

Execution results:

The second way to find IP addresses:

#include <winsock.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")//for linking to ws2_32.lib this library
void Checkip (void)//checkip function for obtaining native IP addresses
{
 WORD wversionrequested;//word type variable, for storing Winsock version of value
 wsadata wsadata;
 The char name[255];//is used to store the host name
 phostent hostinfo;
 Wversionrequested=makeword (2,0);
 Call the Makeword () function to get the Winsock version to load the Winsock library
 if (WSAStartup (wversionrequested,&wsadata) = 0)
 {
 // Loading the Winsock library, if the WSAStartup () function returns a value of 0, indicates that the load success
 if (GetHostName (name,sizeof (name) ==0)
 {
  if (Hostinfo = gethostbyname (name)!= NULL)
  {
  //If the host name succeeds, invoke the Inet_ntoa () function to obtain the IP address
  lpcstr IP = inet_ntoa (* (struct in _ADDR *) *hostinfo->h_addr_list);
  printf ("The IP address of this machine is:%s\n", IP);//output IP address
  printf ("The name of this machine is:%s\n,", name);
  }
 }
 WSACleanup ();
 }
int main ()
{
 checkip ();//Call Checkip () function to obtain and output IP address return
 0;

}

The above is a small series for everyone to get the C + + access to all the IP card and MAC address information of the implementation of all the content, I hope that we support cloud-Habitat Community ~

Related Article

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.