#include <WinSock2.h>
#include <Iphlpapi.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
int mainxxxx(void)
{
//PIP_ADAPTER_INFO结构体指针存储本机网卡信息
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
//得到结构体大小,用于GetAdaptersInfo参数
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
//调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
//TRACE("card num5 = %d\n", sizeof(pIpAdapterInfo->HaveWins));
//记录网卡数量
int netCardNum = 0;
//记录每张网卡上的IP地址数量
int IPnumPerNetCard = 0;
if (ERROR_BUFFER_OVERFLOW == nRel)
{
//如果函数返回的是ERROR_BUFFER_OVERFLOW
//则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
//这也是说明为什么stSize既是一个输入量也是一个输出量
//释放原来的内存空间
delete pIpAdapterInfo;
//重新申请内存空间用来存储所有网卡信息
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
//再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
}
if (ERROR_SUCCESS == nRel){
//输出网卡信息 可能有多网卡,因此通过循环去判断
while (pIpAdapterInfo){
++netCardNum;
TRACE("\n\ncard num = %d\n", netCardNum);
//TRACE("card name = %s\n", pIpAdapterInfo->AdapterName);
TRACE("card desc = %s\n", pIpAdapterInfo->Description);
switch (pIpAdapterInfo->Type)
{
case MIB_IF_TYPE_OTHER:
TRACE("card type = %s\n", "OTHER");
break;
case MIB_IF_TYPE_ETHERNET:
//TRACE("card type = %s\n", "ETHERNET");
break;
case MIB_IF_TYPE_TOKENRING:
TRACE("card type = %s\n", "TOKENRING");
break;
case MIB_IF_TYPE_FDDI:
TRACE("card type = %s\n", "FDDI");
break;
case MIB_IF_TYPE_PPP:
printf("PP\n");
TRACE("card type = %s\n", "PPP");
break;
case MIB_IF_TYPE_LOOPBACK:
TRACE("card type = %s\n", "LOOPBACK");
break;
case MIB_IF_TYPE_SLIP:
TRACE("card type = %s\n", "SLIP");
break;
default:
break;
}
//TRACE("MAC: \n");
//HexDump((char *)&pIpAdapterInfo->Address[0], pIpAdapterInfo->AddressLength, 0);
//可能网卡有多IP,因此通过循环去判断
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
IPnumPerNetCard = 0;
do {
++IPnumPerNetCard;
TRACE("IP = %s\n", pIpAddrString->IpAddress.String);
//TRACE("MASK = %s\n", pIpAddrString->IpMask.String);
TRACE("GATEWAY = %s\n", pIpAdapterInfo->GatewayList.IpAddress.String );
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
pIpAdapterInfo = pIpAdapterInfo->Next;
}
}
//释放内存空间
if (pIpAdapterInfo){
delete pIpAdapterInfo;
}
return 0;
}
//杂项测试2
void Cfile_extractDlg::OnBnClickedButton7()
{
NCB ncb;
int num;
ASTAT Adapter;
LANA_ENUM lana_enum;
//取得网卡信息列表
UCHAR uRetCode;
num = get_ethernet_card_num();
TRACE("num = %d\n", num);
//对每一个网卡,以其网卡编号为输入编号,获取其MAC地址
memset(&ncb, 0, sizeof(ncb));
memset(&lana_enum, 0, sizeof(lana_enum));
ncb.ncb_command = NCBENUM; //统计系统中网卡的数量
ncb.ncb_buffer = (unsigned char *)&lana_enum;
ncb.ncb_length = sizeof(LANA_ENUM);
//向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡,每个网卡的编号(MAC地址)
uRetCode = Netbios(&ncb);
if (uRetCode != NRC_GOODRET){
AfxMessageBox(_T("error"));
}
for (int lana = 0; lana < lana_enum.length; lana++){
//准备取得接口卡的状态块取得MAC地址
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBASTAT; //对网卡发送NCBSTAT命令,获取网卡信息
ncb.ncb_lana_num = lana_enum.lana[lana]; //指定网卡号,这里仅仅指定第一块网卡,通常为有线网卡
strcpy((char*)ncb.ncb_callname, "*"); //远程系统名赋值为*
ncb.ncb_buffer = (unsigned char *)&Adapter; //指定返回的信息存放的变量
ncb.ncb_length = sizeof(Adapter);
//接着发送NCBASTAT命令以获取网卡的信息
uRetCode = Netbios(&ncb);
//取得网卡的信息,并且如果网卡正常工作的话,返回标准的冒号分隔格式。
if (uRetCode != NRC_GOODRET){
AfxMessageBox(_T("error"));
}
//把网卡MAC地址格式转化为常用的16进制形式,输出到字符串
CString strLocalMac;
strLocalMac.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5]
);
//AfxMessageBox(strLocalMac);
}
mainxxxx();
//PIP_ADAPTER_INFO结构体指针存储本机网卡信息
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
//得到结构体大小,用于GetAdaptersInfo参数
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
//调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
//记录网卡数量
DWORD netCardNum = 0;
GetNumberOfInterfaces(&netCardNum);
//记录每张网卡上的IP地址数量
int IPnumPerNetCard = 0;
if (ERROR_BUFFER_OVERFLOW == nRel)
{
//如果函数返回的是ERROR_BUFFER_OVERFLOW
//则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
//这也是说明为什么stSize既是一个输入量也是一个输出量
//释放原来的内存空间
delete pIpAdapterInfo;
//重新申请内存空间用来存储所有网卡信息
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
//再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
}
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
//本地ip地址
//CString strIpAddr = pIpAddrString->IpAddress.String;
//释放资源
delete pIpAdapterInfo;
pIpAdapterInfo = nullptr;