获取本地MAC地址和多IP
1 #include<atlbase.h> 2 #include<atlconv.h> 3 #include"iphlpapi.h" 4 #pragma comment(lib,"Iphlpapi.lib") 5 void Get_IP() 6 { 7 PIP_ADAPTER_INFO pAdapterInfo; 8 PIP_ADAPTER_INFO pAdapter = NULL; 9 DWORD dwRetVal = 0; 10 pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO)); 11 ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); 12 if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS){ 13 //GlobalFree(pAdapterInfo); 14 pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); 15 } 16 if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR){ 17 pAdapter = pAdapterInfo; 18 while (pAdapter){ 19 if (strstr(pAdapter->Description, "PCI")>0//pAdapter->Description中包含"PCI"为:物理网卡 20 || pAdapter->Type == 71//pAdapter->Type是71为:无线网卡 21 ){ 22 printf("------------------------------------------------------------\n"); 23 printf("网卡名称:\t%s\n", pAdapter->AdapterName); 24 printf("网卡描述:\t%s\n", pAdapter->Description); 25 printf("Gateway:\t%s\n", pAdapter->GatewayList.IpAddress.String); 26 printf("网卡MAC地址:\t"); 27 for (UINT i = 0; i<pAdapter->AddressLength; i++){ 28 printf("%02X%c", pAdapter->Address[i], 29 i == pAdapter->AddressLength - 1 ? '\n' : '-'); 30 } 31 32 printf("网卡类型(71为无线网卡,6为物理网卡):\t%d\n", pAdapter->Type); 33 printf("IP地址:\t%s\n", pAdapter->IpAddressList.IpAddress.String); 34 printf("子网掩码:\t%s\n", pAdapter->IpAddressList.IpMask.String); 35 } 36 pAdapter = pAdapter->Next; 37 } 38 } 39 else 40 { 41 printf("Call to Get Adapters Information failed\n"); 42 } 43 } 44 45 46 void main() 47 { 48 Get_IP(); 49 }
http://blog.csdn.net/l00109503/article/details/4044189