获取本地网卡适配器信息

效果如下:

具体代码如下:

  1 #include <Windows.h>
  2 #include <IPHlpApi.h>
  3 #include <stdio.h>
  4 
  5 #pragma comment(lib, "IPHlpApi")
  6 #pragma comment(lib, "ws2_32")
  7 
  8 int main(int argc, char **argv)
  9 {
 10     PIP_ADAPTER_INFO pAdapterInfo = NULL;
 11     ULONG ulLen = sizeof(IP_ADAPTER_INFO);
 12     struct tm newtime;
 13     char szBuffer[32];
 14     errno_t error;
 15 
 16     //为适配器结构申请内存
 17     //pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, ulLen);
 18     pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
 19     if (NULL == pAdapterInfo)
 20     {
 21         printf("Error allocating memory needed to call GetAdaptersInfo.
");
 22         return 1;
 23     }
 24 
 25     if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pAdapterInfo, &ulLen))
 26     {
 27         HeapFree(GetProcessHeap(), 0, pAdapterInfo);
 28         pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, ulLen);
 29         if (NULL == pAdapterInfo)
 30         {
 31             printf("Error allocating memory needed to call GetAdaptersInfo.
");
 32             return 1;
 33         }
 34     }
 35 
 36     //取得本地适配器结构信息
 37     if (ERROR_SUCCESS != GetAdaptersInfo(pAdapterInfo, &ulLen))
 38     {
 39         printf("GetAdaptersInfo error!
");
 40         return 0;
 41     }
 42     if (NULL == pAdapterInfo)
 43     {
 44         printf("There is no adapters!
");
 45         return 0;
 46     }
 47 
 48     SetConsoleTitle(TEXT("本地网卡适配器信息"));
 49 
 50     do
 51     {
 52         printf("ComboIndex:%d
", pAdapterInfo->ComboIndex);
 53         printf("Adapter Name:%s
", pAdapterInfo->AdapterName);
 54         printf("Adapter Desc:%s
", pAdapterInfo->Description);
 55         printf("Adapter Addr:");
 56         for (size_t i = 0; i < pAdapterInfo->AddressLength; i++)
 57         {
 58             if (i == (pAdapterInfo->AddressLength - 1))
 59             {
 60                 printf("%02X", (int)pAdapterInfo->Address[i]);
 61             }
 62             else
 63             {
 64                 printf("%02X-", (int)pAdapterInfo->Address[i]);
 65             }
 66         }
 67         printf("
");
 68         printf("Index:%d
", pAdapterInfo->Index);
 69         printf("Type:");
 70         switch (pAdapterInfo->Type)
 71         {
 72         case MIB_IF_TYPE_OTHER:printf("Other
"); break;
 73         case MIB_IF_TYPE_ETHERNET:printf("Ethernet
"); break;
 74         case MIB_IF_TYPE_TOKENRING:printf("Token Ring
"); break;
 75         case MIB_IF_TYPE_FDDI:printf("FDDI
"); break;
 76         case MIB_IF_TYPE_PPP:printf("PPP
"); break;
 77         case MIB_IF_TYPE_LOOPBACK:printf("Lookback
"); break;
 78         case MIB_IF_TYPE_SLIP:printf("Slip
"); break;
 79         default:printf("Unknow type %ld
", pAdapterInfo->Type); break;
 80         }
 81         printf("IP Address:%s
", pAdapterInfo->IpAddressList.IpAddress.String);
 82         printf("IP Mask:%s
", pAdapterInfo->IpAddressList.IpMask.String);
 83         printf("Gateway:%s
", pAdapterInfo->GatewayList.IpAddress.String);
 84 
 85         if (pAdapterInfo->DhcpEnabled)
 86         {
 87             printf("DHCP Enabled:Yes
");
 88             printf("DHCP Server:%s
", pAdapterInfo->DhcpServer.IpAddress.String);
 89             printf("Lease Obtained:");
 90             error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseObtained);
 91             if (error)
 92             {
 93                 printf("Invalid Argument to _localtime32_s.
");
 94             }
 95             else
 96             {
 97                 error = asctime_s(szBuffer, 32, &newtime);
 98                 if (error)
 99                 {
100                     printf("Invalid Argument to asctime_s.
");
101                 }
102                 else
103                 {
104                     printf("%s", szBuffer);
105                 }
106             }
107 
108             printf("Lease Expires:");
109             error = _localtime32_s(&newtime, (__time32_t*)&pAdapterInfo->LeaseExpires);
110             if (error)
111             {
112                 printf("Invalid Argument to _localtime32_s.
");
113             }
114             else
115             {
116                 error = asctime_s(szBuffer, 32, &newtime);
117                 if (error)
118                 {
119                     printf("Invalid Argument to asctime_s.
");
120                 }
121                 else
122                 {
123                     printf("%s", szBuffer);
124                 }
125             }
126         }
127         else
128         {
129             printf("DHCP Enabled:No
");
130         }
131 
132         if (pAdapterInfo->HaveWins)
133         {
134             printf("Have Wins:Yes
");
135             printf("Primary Wins Server:%s
", pAdapterInfo->PrimaryWinsServer.IpAddress.String);
136             printf("Secondary Wins Server:%s
", pAdapterInfo->SecondaryWinsServer.IpAddress.String);
137         }
138         else
139         {
140             printf("Have Wins:No
");
141         }
142 
143         printf("=================================================================
");
144 
145         pAdapterInfo = pAdapterInfo->Next;
146     } while (pAdapterInfo);
147 
148     if (pAdapterInfo)
149     {
150         HeapFree(GetProcessHeap(), 0, pAdapterInfo);
151     }
152 
153     return 0;
154 }
原文地址:https://www.cnblogs.com/qiyueliuguang/p/3498549.html