/////////////////////////////////////////
//
// FileName : NetCardVer.cpp
// Creator : PeterZ
// Date : 2018-6-21 23:50
// Comment : 网卡信息筛选
// Editor : Visual Studio 2017
//
/////////////////////////////////////////
#include "NetInfoProc.h"
void Output1(PIP_ADAPTER_INFO pIpAdapterInfo); //结果输出1(正常结果)
void Output2(PIP_ADAPTER_INFO pIpAdapterInfo); //结果输出2(删除虚拟网卡的结果)
void Output3(PIP_ADAPTER_INFO pIpAdapterInfo); //结果输出3(去除非PCI物理网卡) >>需要以管理员权限运行程序<<
void Output4(PIP_ADAPTER_INFO pIpAdapterInfo); //结果输出4(筛选内网网卡)
//主函数
int main(void)
{
PIP_ADAPTER_INFO pIpAdapterInfo = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
if (ERROR_BUFFER_OVERFLOW == nRel/*GetAdaptersInfo参数传递的内存空间不足*/)
{
//free(pIpAdapterInfo);
pIpAdapterInfo = (PIP_ADAPTER_INFO)realloc(pIpAdapterInfo, stSize);
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
}
if (ERROR_SUCCESS == nRel)
{
printf(">>>>>>>>> 正常结果 <<<<<<<<<<<\n\n");
Output1(pIpAdapterInfo);
printf("\n\n>>>>>>>>> 删除虚拟网卡的结果 <<<<<<<<<\n\n");
Output2(pIpAdapterInfo);
printf("\n\n>>>>>>>>> 去除非PCI物理网卡的结果 <<<<<<<<<\n\n");
Output3(pIpAdapterInfo);
printf("\n\n>>>>>>>>> 筛选内网网卡的结果 <<<<<<<<<\n\n");
Output4(pIpAdapterInfo);
}
if (pIpAdapterInfo)
{
free(pIpAdapterInfo);
}
system("pause");
return 0;
}
//结果输出1(正常结果)
void Output1(PIP_ADAPTER_INFO pIpAdapterInfo)
{
//可能有多网卡,因此通过循环去判断
while (pIpAdapterInfo)
{
//输出信息
cout << "网卡名称:" << pIpAdapterInfo->AdapterName << endl;
cout << "网卡描述:" << pIpAdapterInfo->Description << endl;
cout << "网卡MAC地址:" << pIpAdapterInfo->Address;
for (UINT i = 0; i < pIpAdapterInfo->AddressLength; i++)
{
if (i == pIpAdapterInfo->AddressLength - 1)
{
printf("%02x\n", pIpAdapterInfo->Address[i]);
}
else
{
printf("%02x-", pIpAdapterInfo->Address[i]);
}
}
cout << "网卡IP地址如下:" << endl;
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
//可能网卡有多IP,因此通过循环去判断
do
{
cout << pIpAddrString->IpAddress.String << endl;
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
pIpAdapterInfo = pIpAdapterInfo->Next;
cout << "*****************************************************" << endl;
}
return;
}
//结果输出2(删除虚拟网卡的结果)
void Output2(PIP_ADAPTER_INFO pIpAdapterInfo)
{
//可能有多网卡,因此通过循环去判断
while (pIpAdapterInfo)
{
//去除虚拟网卡IP
if (IsVirtualNetCard(pIpAdapterInfo))
{
pIpAdapterInfo = pIpAdapterInfo->Next;
continue;
}
//输出信息
cout << "网卡名称:" << pIpAdapterInfo->AdapterName << endl;
cout << "网卡描述:" << pIpAdapterInfo->Description << endl;
cout << "网卡MAC地址:" << pIpAdapterInfo->Address;
for (UINT i = 0; i < pIpAdapterInfo->AddressLength; i++)
{
if (i == pIpAdapterInfo->AddressLength - 1)
{
printf("%02x\n", pIpAdapterInfo->Address[i]);
}
else
{
printf("%02x-", pIpAdapterInfo->Address[i]);
}
}
cout << "网卡IP地址如下:" << endl;
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
//可能网卡有多IP,因此通过循环去判断
do
{
cout << pIpAddrString->IpAddress.String << endl;
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
pIpAdapterInfo = pIpAdapterInfo->Next;
cout << "*****************************************************" << endl;
}
return;
}