今天做一个告警检测时,需要获取本机物理网卡的所有信息,查阅MSDN后,发现可以使用windows api:GetAdaptersAddresses().
接口:
ULONG WINAPI GetAdaptersAddresses(
__in ULONG Family, //指定网络协议是IPv4还是IPv6
__in ULONG Flags, //指定地址类型的,可以指定单播、多播、ipv6、DNS等
__in PVOID Reserved, //保留参数,传NULL
__inout PIP_ADAPTER_ADDRESSES AdapterAddresses, //指向网卡信息结构体的指针,是一个链表
__inout PULONG SizePointer //dapterAddresses所需数据大小的值
);
下面是使用例子:
1 #include "stdafx.h" 2 #include <winsock2.h> 3 #include <ws2tcpip.h> 4 #pragma comment(lib, "IPHLPAPI.lib") 5 #pragma comment(lib, "Ws2_32.lib") 6 #include <iphlpapi.h> 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 12 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 13 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) 14 15 /* Note: could also use malloc() and free() */ 16 17 int main(int argc, TCHAR* argv[]) 18 { 19 20 // Declare and initialize variables. 21 DWORD dwSize = 0; 22 DWORD dwRetVal = 0; 23 24 unsigned int i, j; 25 26 //////////////////////////////////////////////// 27 PIP_ADAPTER_ADDRESSES pAddresses = NULL, pAdapter = NULL; 28 ULONG outBufLen = 1024*15; 29 pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen); 30 if (pAddresses == NULL) 31 { 32 printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n"); 33 exit(1); 34 } 35 dwRetVal = GetAdaptersAddresses(AF_INET,GAA_FLAG_INCLUDE_PREFIX,NULL, pAddresses, &outBufLen); 36 if (pAddresses) 37 { 38 pAdapter = pAddresses; 39 } 40 while (pAdapter) 41 { 42 if (pAdapter->OperStatus == IfOperStatusUp && pAdapter->IfType == IF_TYPE_ETHERNET_CSMACD) 43 { 44 printf("Operation Status:%d\n", pAdapter->OperStatus); 45 printf("Name:%s\n", pAdapter->AdapterName); 46 printf("Desc:%ws\n", pAdapter->Description); 47 printf("Type:%d\n",pAdapter->IfType); 48 printf("Desc:%llu\n", pAdapter->TransmitLinkSpeed); 49 printf("Desc:%llu\n", pAdapter->ReceiveLinkSpeed); 50 51 PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pAdapter->FirstUnicastAddress; 52 char buff[100] = {0}; 53 DWORD bufflen = 100; 54 for (i = 0; pUnicast != NULL; i++) 55 { 56 if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) 57 { 58 sockaddr_in *sa_in = (sockaddr_in *)pUnicast->Address.lpSockaddr; 59 printf("IPV4 Unicast Address:%s\n",inet_ntoa(sa_in->sin_addr)); 60 } //inet_ntop 61 else if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) 62 { 63 sockaddr_in6 *sa_in6 = (sockaddr_in6 *)pUnicast->Address.lpSockaddr; 64 printf("IPV6 not support!!!\n"); 65 } //inet_ntop 66 else 67 { 68 printf("\tUNSPEC"); 69 } 70 pUnicast = pUnicast->Next; 71 } 72 printf("Number of Unicast Addresses: %d\n", i); 73 74 if (pAdapter->PhysicalAddressLength != 0) 75 { 76 printf("\tPhysical address: "); 77 for (i = 0; i < (int) pAdapter->PhysicalAddressLength;i++) 78 { 79 if (i == (pAdapter->PhysicalAddressLength - 1)) 80 printf("%.2X\n", 81 (int) pAdapter->PhysicalAddress[i]); 82 else 83 printf("%.2X-", 84 (int) pAdapter->PhysicalAddress[i]); 85 } 86 } 87 } 88 pAdapter = pAdapter->Next; 89 } 90 91 //////////////////////////////////////////////// 92 if (pAddresses) 93 { 94 FREE(pAddresses); 95 } 96 97 return 0; 98 }
控制台输出如图: