判断多网卡是否连接到网络

这是MSDN提供的一个实例程序,主要演示函数GetAdaptersAddresses的用法。

精简了一下,只查看得到结构体IP_ADAPTER_ADDRESSES中的一个成员OperStatus,就可以区分机器上安装的多个网卡的连接情况。

先来看看结构体IP_ADAPTER_ADDRESSES:

typedef struct _IP_ADAPTER_ADDRESSES {
  union {
    ULONGLONG Alignment;
    struct {
      ULONG Length;
      DWORD IfIndex;
    } ;
  } ;
  struct _IP_ADAPTER_ADDRESSES *Next;
  PCHAR                              AdapterName;
  PIP_ADAPTER_UNICAST_ADDRESS        FirstUnicastAddress;
  PIP_ADAPTER_ANYCAST_ADDRESS        FirstAnycastAddress;
  PIP_ADAPTER_MULTICAST_ADDRESS      FirstMulticastAddress;
  PIP_ADAPTER_DNS_SERVER_ADDRESS     FirstDnsServerAddress;
  PWCHAR                             DnsSuffix;
  PWCHAR                             Description;
  PWCHAR                             FriendlyName;
  BYTE                               PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH];
  DWORD                              PhysicalAddressLength;
  DWORD                              Flags;
  DWORD                              Mtu;
  DWORD                              IfType;
  IF_OPER_STATUS                     OperStatus;
  DWORD                              Ipv6IfIndex;
  DWORD                              ZoneIndices[16];
  PIP_ADAPTER_PREFIX                 FirstPrefix;
  ULONG64                            TransmitLinkSpeed;
  ULONG64                            ReceiveLinkSpeed;
  PIP_ADAPTER_WINS_SERVER_ADDRESS_LH FirstWinsServerAddress;
  PIP_ADAPTER_GATEWAY_ADDRESS_LH     FirstGatewayAddress;
  ULONG                              Ipv4Metric;
  ULONG                              Ipv6Metric;
  IF_LUID                            Luid;
  SOCKET_ADDRESS                     Dhcpv4Server;
 

NET_IF_COMPARTMENT_ID CompartmentId; NET_IF_NETWORK_GUID NetworkGuid; NET_IF_CONNECTION_TYPE ConnectionType; TUNNEL_TYPE TunnelType; SOCKET_ADDRESS Dhcpv6Server; BYTE Dhcpv6ClientDuid[MAX_DHCPV6_DUID_LENGTH]; ULONG Dhcpv6ClientDuidLength; ULONG Dhcpv6Iaid; PIP_ADAPTER_DNS_SUFFIX FirstDnsSuffix;} IP_ADAPTER_ADDRESSES, *PIP_ADAPTER_ADDRESSES;

其中对我们有用的有IfType和OperStatus,ifType可以用来判断查询出的网卡是哪种类型,文档中是这么写的:

The interface type as defined by the Internet Assigned Names Authority (IANA). Possible values for the interface type are listed in the Ipifcons.h header file. 

The table below lists common values for the interface type although many other values are possible. 

Value Meaning 
IF_TYPE_OTHER
1 Some other type of network interface.
 
IF_TYPE_ETHERNET_CSMACD
6 An Ethernet network interface.
 
IF_TYPE_ISO88025_TOKENRING
9 A token ring network interface.
 
IF_TYPE_PPP
23 A PPP network interface.
 
IF_TYPE_SOFTWARE_LOOPBACK
24 A software loopback network interface.
 
IF_TYPE_ATM
37 An ATM network interface.
 
IF_TYPE_IEEE80211
71 An IEEE 802.11 wireless network interface. On Windows Vista and later, wireless network cards are reported as IF_TYPE_IEEE80211. On earlier versions of Windows, wireless network cards are reported as IF_TYPE_ETHERNET_CSMACD.
 
IF_TYPE_TUNNEL
131 A tunnel type encapsulation network interface.
 
IF_TYPE_IEEE1394
144 An IEEE 1394 (Firewire) high performance serial bus network interface.
 
OperStatus可以用来判断连接状态:

The operational status for the interface as defined in RFC 2863. For more information, see http://www.ietf.org/rfc/rfc2863.txt. This member can be one of the values from the IF_OPER_STATUS enumeration type defined in the Iftypes.h header file. On Windows Vista and later, the header files were reorganized and this enumeration is defined in the Ifdef.h header file.

Value Meaning 
IfOperStatusUp
1 The interface is up and able to pass packets.
 
IfOperStatusDown
2 The interface is down and not in a condition to pass packets. The IfOperStatusDown state has two meanings, depending on the value of AdminStatus member. If AdminStatus is not set to NET_IF_ADMIN_STATUS_DOWN and ifOperStatus is set to IfOperStatusDown then a fault condition is presumed to exist on the interface. If AdminStatus is set to IfOperStatusDown, then ifOperStatus will normally also be set to IfOperStatusDown or IfOperStatusNotPresent and there is not necessarily a fault condition on the interface. 
 




#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "IPHLPAPI.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int __cdecl main(int argc, char **argv)
{
	DWORD dwSize = 0;
	DWORD dwRetVal = 0;
	int i = 0;
	ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
	ULONG family = AF_INET;
	LPVOID lpMsgBuf = NULL;
	PIP_ADAPTER_ADDRESSES pAddresses = NULL;
	ULONG outBufLen = 0;
	PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
	outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
	pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
	if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)
		== ERROR_BUFFER_OVERFLOW) {
			FREE(pAddresses);
			pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
	}

	if (pAddresses == NULL) {
		exit(1);
	}
	dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);

	if (dwRetVal == NO_ERROR) {
		pCurrAddresses = pAddresses;
		while (pCurrAddresses) {
			printf("\tDescription: %wS\n", pCurrAddresses->Description);
			printf("\tIfType: %ld\n", pCurrAddresses->IfType);
			printf("\tOperStatus: %ld\n", pCurrAddresses->OperStatus);
			printf("\n");
			pCurrAddresses = pCurrAddresses->Next;
		}
	}
	FREE(pAddresses);
	return 0;
}


下面是我测试的输出结果:

        Description: Microsoft Virtual WiFi Miniport Adapter
        IfType: 71
        OperStatus: 2

        Description: Atheros AR8151 PCI-E Gigabit Ethernet Controller (NDIS 6.20)
        IfType: 6
        OperStatus: 1

        Description: Intel(R) Centrino(R) Wireless-N 100
        IfType: 71
        OperStatus: 2

        Description: Software Loopback Interface 1
        IfType: 24
        OperStatus: 1

Press any key to continue . . .
这是当时的网卡状态,Ethernet是有线网卡,已经连接到路由器OperStatus值是1,Wireless是无线网卡,当前是关闭状态,所以得出的OperStatus值是2,Virtual WiFi是个虚拟网卡,也没有连接。
判断多网卡是否连接到网络_第1张图片

你可能感兴趣的:(struct,server,Microsoft,NetWork,interface,alignment)