Get the network card name, network card description, network card MAC address, network card IP, network card type and other information and whether the network cable is plugged into the state

Source: Internet
Author: User
Tags ip number

using the API functions provided by the Windows SDK GetAdaptersInfo () can obtain the network card name of all network cards, network card description, network card MAC address, network card IP, network card type and other information, and ip_adapter_info structure storage, Use Getifentry () to obtain the state of the network card, can effectively determine whether the network card communication is normal, whether to insert network cable and other states ...

For more detailed instructions and code examples in this article see: http://download.csdn.net/detail/lusirking/9562517

determine if the network cable is plugged in

You can use the Getifentry () in Iphelpapi to get the code as follows:

BOOL Getadapterstate (DWORD index)
{
Mib_ifrow Info; To store the adapter parameters obtained
memset (&info, 0, sizeof (mib_ifrow));
Info.dwindex = index; Dwindex is the index of the adapter that needs to be obtained and can be obtained by getadaptersinfo and other related functions
if (Getifentry (&info)!= noerror)
{
printf ("ErrorCode =%d\n", GetLastError ());
return false;
}
if (Info.dwoperstatus = = If_oper_status_non_operational

|| Info.dwoperstatus = = If_oper_status_unreachable
|| Info.dwoperstatus = = if_oper_status_disconnected

|| Info.dwoperstatus = = if_oper_status_connecting)
return false;
else if (Info.dwoperstatus = = If_oper_status_operational

|| Info.dwoperstatus = = if_oper_status_connected)
return true;
}

Mib_ifrow Introduction:

There is a dwoperstatus parameter in the Mib_ifrow that indicates the operation state of the current interface

The values are as follows:

1) if_oper_status_non_operational

LanAdapter has been disabled, for example because of a address conflict.

LAN adapter disabled, such as address conflicts

2) if_oper_status_unreachable

Wanadapter that isn't connected.

WAN Adapter Not connected

3) if_oper_status_disconnected

Forlan adapters:network Cable Disconnected. For WAN Adapters:no Carrier

LAN Adapter: The network cable is not plugged in. WAN Adapters: No signal

4) if_oper_status_connecting

Wanadapter that's in the process of connecting.

Processing connection

5) if_oper_status_connected

Wanadapter is connected to a remote peer.

is connected to the remote

6) if_oper_status_operational

Defaultstatus for LAN adapters default state

after testing

when disconnecting the network cable, the Dwoperstatus value is if_oper_status_non_operational

when connecting a network cable, the Dwoperstatus value is if_oper_status_operational

The ip_adapter_info structure is described as follows:

typedef struct _IP_ADAPTER_INFO

{

struct _ip_adapter_info* next;//Pointer to the next adapter information in the list

DWORD comboindex;//Reservation Value

Char Adaptername[max_adapter_name_length + 4];//adapter name represented using ANSI string

Char description[max_adapter_description_length +4];//Adapter description using an ANSI string representation

UINT the length of the addresslength;//adapter hardware address in bytes

The byte address[max_adapter_address_length];//hardware address is represented as a byte array

DWORD index;//Adapter Index

UINT type;//Adapter types, mainly in the following categories:

/*

* Mib_if_type_other 1

* Mib_if_type_ethernet 6

* Mib_if_type_tokenring 9

* Mib_if_type_fddi 15

* MIB_IF_TYPE_PPP 23

* Mib_if_type_loopback 24

* Mib_if_type_slip 28

*/

UINT dhcpenabled;//Specifies whether this adapter will turn on DHCP

Pip_addr_string currentipaddress;//Reserved value

Ip_addr_string ipaddresslist;//The adapter's IPV4 address List

Ip_addr_string gatewaylist;//The adapter's Gateway IPV4 address List

Ip_addr_string the IPV4 address list of the DHCP server dhcpserver;//the adapter

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;

Ip_addr_string structure is also a linked list node

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


the code to get the NIC information 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 __cdecl Main ()
{
Pip_adapter_info structure body pointers Store native network card information
Pip_adapter_info pipadapterinfo = new Ip_adapter_info ();
Pip_adapter_info padapter = NULL;
The structure body size is obtained and used for GetAdaptersInfo parameters
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);
Log 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 a Error_buffer_overflow
Indicates that the GetAdaptersInfo parameter has not passed enough memory space and that it is outgoing stsize, indicating the required space size
This also explains why Stsize is both an input and an output
Free up the original memory space
Delete Pipadapterinfo;
Re-request memory space to store all network card information
Pipadapterinfo = (pip_adapter_info) new byte[stsize];
Call the GetAdaptersInfo function again, populating the Pipadapterinfo pointer variable
Nrel=getadaptersinfo (pipadapterinfo,&stsize);
}
if (error_success = = NRel)
{
Output Network card information
may have multiple network adapters, so pass the loop to determine
Padapter = Pipadapterinfo;
while (Padapter)
{
cout<< "Number of network cards:" <<++netCardNum<<endl;
cout<< "Network card name:" <<pAdapter->AdapterName<<endl;
cout<< "Network card description:" <<pAdapter->Description<<endl;
Switch (padapter->type)
{
Case Mib_if_type_other:
cout<< "Network Adapter Type:" << "Other" <<endl;
Break
Case Mib_if_type_ethernet:
cout<< "network card type:" << "ETHERNET" <<endl;
Break
Case mib_if_type_tokenring:
cout<< "network card type:" << "Tokenring" <<endl;
Break
Case MIB_IF_TYPE_FDDI:
cout<< "Network Adapter 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 card type:" << "loopback" <<endl;
Break
Case Mib_if_type_slip:
cout<< "network card type:" << "SLIP" <<endl;
Break
Default
Break
}
cout<< "Nic mac address:";
for (DWORD i = 0; i < padapter->addresslength; i++)
if (I < padapter->addresslength-1)
{
printf ("%02x-", Padapter->address[i]);
}
Else
{
printf ("%02x\n", Padapter->address[i]);
}
cout<< "network card IP address is as follows:" <<endl;
The network card may have multiple IP, so the loop is used to determine
Ip_addr_string *pipaddrstring =& (padapter->ipaddresslist);
Ipnumpernetcard = 0;
Todo
{
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:" <<pAdapter->GatewayList.IpAddress.String<<endl;
pipaddrstring=pipaddrstring->next;
while (pipaddrstring);
if (Getadapterstate (Padapter->index))
cout<< "Nic working properly" <<endl;
Else
cout<< "NIC work exception" <<endl;
Padapter = padapter->next;
cout<< "--------------------------------------------------------------------" <<endl;
}
}
Free up memory space
if (pipadapterinfo)
{
delete []pipadapterinfo;
Pipadapterinfo=null;
}
return 0;
}

The running interface is as follows:


For more detailed instructions and code examples in this article see: http://download.csdn.net/detail/lusirking/9562517

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.