DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo, //指向一个缓冲区,用来取得IP_ADAPTER_INFO结构列表
PULONG pOutBufLen //指定上面缓冲区大小,如果大小不够,此参数返回所需大小
)
IP_ADAPTER_INFO结构包含了本地计算机网络适配器的信息
typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;//指向链表中下一个适配器(计算机可能有多个适配器)
DWORD ComboIndex; //保留字段
char AdapterName[MAX_ADAPTER_NAME_LENGTH + ];//使用ANSI字符串表示的适配器名称
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + ];//使用ANSI字符串表示的适配器描述
UINT AddressLength;//适配器MAC地址以字节计算的长度(一般6个字节)
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];//硬件地址以BYTE数组所表示
DWORD Index;//适配器索引
UINT Type;//适配器类型,主要有以下几种:
/*
* MIB_IF_TYPE_OTHER 1
* MIB_IF_TYPE_ETHERNET 6
* MIB_IF_TYPE_TOKENRING 9
* MIB_IF_TYPE_FDDI 15
* MIB_IF_TYPE_PPP 23
* MIB_IF_TYPE_LOOPBACK 24
* MIB_IF_TYPE_SLIP 28
*/
UINT DhcpEnabled;//指定这个适配器是否开启DHCP
PIP_ADDR_STRING CurrentIpAddress;//保留字段
IP_ADDR_STRING IpAddressList;//该适配器的IPv4地址链表
IP_ADDR_STRING GatewayList;//该适配器的网关IPv4地址链表
IP_ADDR_STRING DhcpServer;//该适配器的DHCP服务器的IPv4 地址链表
BOOL HaveWins;//该适配器是否使用WINS(Windows Internet名称服务)
IP_ADDR_STRING PrimaryWinsServer;//WINS服务器主IP地址
IP_ADDR_STRING SecondaryWinsServer;//WINS次IP地址
time_t LeaseObtained;//获取当前DCHP租用时间
time_t LeaseExpires;//当前DCHP租用期满时间
} IP_ADAPTER_INFO,*PIP_ADAPTER_INFO;
IP_ADDR_STRING也是一个结构体
typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING* Next;//指向下一个IP地址信息
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;//子网掩码
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING; typedef struct {
char String[ * ];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING; typedef struct {
char String[ * ];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;
完整代码如下
#include <iostream>
#include <windows.h>
#include <Iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib") using namespace std; BOOL GetGlobalData()
{
PIP_ADAPTER_INFO pIPAdapterInfo = new IP_ADAPTER_INFO();
PIP_ADAPTER_INFO adapters = nullptr;
ULONG size = sizeof(IP_ADAPTER_INFO);
//填充pIPadapterInfo变量,其中size既是一个输入量,也是一个输出量
int nRet = GetAdaptersInfo(pIPAdapterInfo, &size);
//记录网卡数量
int netCarNum = ; if (ERROR_BUFFER_OVERFLOW == nRet)
{
//如果返回此参数,说明GetAdaptersInfo参数传递的内存空间大小不够,同时传出size表示需要的内存空间大小
//释放原来的内存空间
delete pIPAdapterInfo;
pIPAdapterInfo = (PIP_ADAPTER_INFO)new byte[size];
//再次调用GetAdaptersInfo填充结构体
nRet = GetAdaptersInfo(pIPAdapterInfo, &size);
} if (ERROR_SUCCESS == nRet)
{
adapters = pIPAdapterInfo;
//输出网卡信息,可能有多卡,需要循环判断
while (adapters)
{
cout << " 网卡数量: " << ++netCarNum << endl;
cout << " 网卡名称: " << adapters->AdapterName << endl;
cout << " 网卡描述: " << adapters->Description << endl;
cout << " 网卡类型: ";
switch (adapters->Type)
{
case MIB_IF_TYPE_OTHER:
cout << "OTHER" << endl;
break;
case MIB_IF_TYPE_ETHERNET:
cout << "ETHERNET" << endl;
break;
case MIB_IF_TYPE_TOKENRING:
cout << "TOKENRING" << endl;
break;
case MIB_IF_TYPE_FDDI:
cout << "FDDI" << endl;
break;
case MIB_IF_TYPE_PPP:
printf("PP\n");
cout << "PPP" << endl;
break;
case MIB_IF_TYPE_LOOPBACK:
cout << "LOOPBACK" << endl;
break;
case MIB_IF_TYPE_SLIP:
cout << "SLIP" << endl;
break;
default: break;
} cout << " 网卡地址MAC: ";
//以XX-XX-XX-XX-XX-XX的形式输出MAC
for (DWORD i = ; i < adapters->AddressLength; ++i)
{
if (i < adapters->AddressLength - )
{
printf("%02X-", adapters->Address[i]);
}
else
{
printf("%02X\n", adapters->Address[i]);
}
} //可能网卡有多IP,因此通过循环去判断
IP_ADDR_STRING *pIpAddrString = &(adapters->IpAddressList);
do
{
//记录每张网卡的IP地址数量
int IPNumPerCar = ;
cout << "该网卡上的IP数量:" << ++IPNumPerCar << endl;
cout << " IP 地址:" << pIpAddrString->IpAddress.String << endl;
cout << " 子网地址:" << pIpAddrString->IpMask.String << endl;
cout << " 网关地址:" << adapters->GatewayList.IpAddress.String << endl;
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
adapters = adapters->Next;
cout << "--------------------------------------------------------------------" << endl;
}
}
//释放分配的内存
if (pIPAdapterInfo)
delete pIPAdapterInfo;
return true;
}