获取MAC地址 (二)

2014-11-24 03:28:17 · 作者: · 浏览: 1
_ETHERNET
&& strstr(pIPAdapterInfo->Description, "Virtual") > 0
)
{
MacAddr.Type = BFSYS_MAC_VIRTUAL;
}
// 其他网卡
else
{
MacAddr.Type = BFSYS_MAC_UNKNOWN;
}

// 是否需要的网卡类型
if ((type & MacAddr.Type) == MacAddr.Type)
{
dwTotalCount += 1;
if (pAddrAry != NULL)
{
pAddrAry->push_back(MacAddr);
}
}

// 遍历下一个Adapter
pIPAdapterInfo = pIPAdapterInfo->Next;
}

// 释放内存
delete[] pBuf;
pBuf = NULL;
pIPAdapterInfo = NULL;

// 返回获取到的数量
return dwTotalCount;
}

// 取得网卡地址信息,返回对应类型网卡的总数量
BFLIB_API
DWORD
BF_GetMacAddr(
IN BFSYS_MAC_T type, // 待取得网卡类型
OUT BFMacAddrArray *pAddrAry // 输出对应网卡类型的地址信息
)
{
// 清除上次信息
if (pAddrAry != NULL)
{
pAddrAry->clear();
}

// 获取Adapter信息
ULONG ulSize = sizeof(IP_ADAPTER_INFO);
BYTE *pBuf = new BYTE[ulSize];
if (pBuf == NULL)
{
return 0;
}
PIP_ADAPTER_INFO pIPAdapterInfo = (PIP_ADAPTER_INFO)pBuf;
DWORD dwRet = ::GetAdaptersInfo(pIPAdapterInfo, &ulSize);

// 缓冲区溢出则重新申请内存
if (ERROR_BUFFER_OVERFLOW == dwRet)
{
delete[] pBuf ;
pBuf = NULL;
pIPAdapterInfo = NULL;
pBuf = new BYTE[ulSize];
if (pBuf == NULL)
{
return 0;
}
pIPAdapterInfo = (PIP_ADAPTER_INFO)pBuf;
dwRet = ::GetAdaptersInfo(pIPAdapterInfo, &ulSize);
}

// 获取Adapter信息失败
if (dwRet != ERROR_SUCCESS)
{
delete[] pBuf;
pBuf = NULL;
pIPAdapterInfo = NULL;
return 0;
}

// 遍历所有网卡信息
DWORD dwTotalCount = 0;
BFMACADDR MacAddr;
while (pIPAdapterInfo != NULL)
{
// 得到MAC地址信息
memset(&MacAddr, 0, sizeof(MacAddr));
MacAddr.AddressLength = pIPAdapterInfo->AddressLength;
for (UINT i=0; iAddressLength; i++)
{
MacAddr.Address[i] = pIPAdapterInfo->Address[i];
}

// 是否无线网卡
if (pIPAdapterInfo->Type == 71)
{
MacAddr.Type = BFSYS_MAC_WIRELESS;
}
// 是否物理网卡
else if ( pIPAdapterInfo->Type == MIB_IF_TYPE_ETHERNET
&& strstr(pIPAdapterInfo->Description, "PCI") > 0
)
{
MacAddr.Type = BFSYS_MAC_PHYSICAL;
}
// 是否虚拟网卡
else if ( pIPAdapterInfo->Type == MIB_IF_TYPE_ETHERNET
&& strstr(pIPAdapterInfo->Description, "Virtual") > 0
)
{
MacAddr.Type = BFSYS_MAC_VIRTUAL;
}
// 其他网卡
else
{
MacAddr.Type = BFSYS_MAC_UNKNOWN;
}

// 是否需要的网卡类型
if ((type & MacAddr.Type) == MacAddr.Type)
{
dwTotalCount += 1;
if (pAddrAry != NULL)
{
pAddrAry->push_back(MacAddr);
}
}

// 遍历下一个Adapter
pIPAdapterInfo = pIPAdapterInfo->Next;
}

// 释放内存
delete[] pBuf;
pBuf = NULL;
pIPAdapterInfo = NULL;

// 返回获取到的数量
return dwTotalCount;
}
需要使用到

#include
#pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库