vc++获取系统网卡列表及IP地址信息

  1 #include "stdafx.h"
  2 #include <Windows.h>
  3 #include <IPHlpApi.h>
  4 #include <iostream>
  5 #pragma comment(lib,"IPHlpApi.lib")
  6 using namespace std;
  7 
  8 BOOL GetLocalAdaptersInfo()
  9 {
 10     //IP_ADAPTER_INFO结构体
 11     PIP_ADAPTER_INFO pIpAdapterInfo = NULL;
 12     pIpAdapterInfo = new IP_ADAPTER_INFO;
 13 
 14     //结构体大小
 15     unsigned long ulSize = sizeof(IP_ADAPTER_INFO);
 16 
 17     //获取适配器信息
 18     int nRet = GetAdaptersInfo(pIpAdapterInfo,&ulSize);
 19 
 20     if (ERROR_BUFFER_OVERFLOW == nRet)
 21     {
 22         //空间不足,删除之前分配的空间
 23         delete []pIpAdapterInfo;
 24 
 25         //重新分配大小
 26         pIpAdapterInfo = (PIP_ADAPTER_INFO) new BYTE[ulSize];
 27 
 28         //获取适配器信息
 29         nRet = GetAdaptersInfo(pIpAdapterInfo,&ulSize);
 30 
 31         //获取失败
 32         if (ERROR_SUCCESS  != nRet)
 33         {
 34             if (pIpAdapterInfo != NULL)
 35             {
 36                 delete []pIpAdapterInfo;
 37             }
 38             return FALSE;
 39         }
 40     }
 41 
 42     //MAC 地址信息
 43     char szMacAddr[20];
 44     //赋值指针
 45     PIP_ADAPTER_INFO pIterater = pIpAdapterInfo;
 46     while(pIterater)
 47     {
 48         cout<<"网卡名称:"<<pIterater->AdapterName<<endl;
 49 
 50         cout<<"网卡描述:"<<pIterater->Description<<endl;
 51 
 52         sprintf_s(szMacAddr,20,"%02X-%02X-%02X-%02X-%02X-%02X",
 53                                               pIterater->Address[0],
 54                                               pIterater->Address[1],
 55                                               pIterater->Address[2],
 56                                               pIterater->Address[3],
 57                                               pIterater->Address[4],
 58                                               pIterater->Address[5]);
 59 
 60         cout<<"MAC 地址:"<<szMacAddr<<endl;
 61 
 62         cout<<"IP地址列表:"<<endl<<endl;
 63 
 64         //指向IP地址列表
 65         PIP_ADDR_STRING pIpAddr = &pIterater->IpAddressList;
 66         while (pIpAddr)
 67         {
 68             cout<<"IP地址:  "<<pIpAddr->IpAddress.String<<endl;
 69             cout<<"子网掩码:"<<pIpAddr->IpMask.String<<endl;
 70 
 71             //指向网关列表
 72             PIP_ADDR_STRING pGateAwayList = &pIterater->GatewayList;
 73             while(pGateAwayList)
 74             {
 75                 cout<<"网关:    "<<pGateAwayList->IpAddress.String<<endl;
 76 
 77                 pGateAwayList = pGateAwayList->Next;
 78             }
 79 
 80             pIpAddr = pIpAddr->Next;
 81         }
 82         cout<<endl<<"--------------------------------------------------"<<endl;
 83 
 84         pIterater = pIterater->Next;
 85     }
 86 
 87     //清理
 88     if (pIpAdapterInfo)
 89     {
 90         delete []pIpAdapterInfo;
 91     }
 92 
 93     return TRUE;
 94 }
 95 
 96 int _tmain(int argc, _TCHAR* argv[])
 97 {
 98     GetLocalAdaptersInfo();
 99 
100     cin.get();
101     return 0;
102 }

 

posted @ 2016-08-25 21:59  南下玩技术  阅读(3582)  评论(0编辑  收藏  举报