API函数GetAdaptersAddresses获取网卡信息MAC &IP &描述信息

本文介绍了如何使用API函数GetAdaptersAddresses替代GetAdaptersInfo来获取本地计算机的网卡信息,包括MAC地址、IP地址和描述信息。在Windows 8上,GetAdaptersAddresses能正确处理蓝牙开启的情况。文章还讨论了如何通过Description字段过滤掉蓝牙设备,并解决了在输出汉字描述时的字符集问题。

一 , 两个函数

The GetAdaptersInfo function retrieves adapter information for the local computer

The GetAdaptersAddresses function retrieves the addresses associated with the adapters on the local computer.

msdn上说:  On Windows XP and later:  Use the GetAdaptersAddresses function instead of GetAdaptersInfo.

之前一直使用GetAdaptersInfo() 获得本机网卡信息, 用在win8上有个问题, 蓝牙开启时读不到资料. 改成GetAdaptersAddresses可以正常读信息.

 

二 网卡类型

通常用 pCurrAddresses->IfType来判断网卡类型, 但实际用时发现蓝牙的类型IF_TYPE_ETHERNET_CSMACD , 也会被当做以太网卡 , 这样就会找到两个以太网信息.我使用pCurrAddresses->Description来屏蔽蓝牙设备.

if(!wcsstr(pCurrAddresses->Description,_T("Bluetooth"))) //the adapter is not bluetooth

 

三 汉字输出

pCurrAddresses->Description的描述中会有汉字字符, 直接用wcout是没办法输出的 (也不会报错) . 解决办法是设置console的输出字符集

         CString temp;
         temp.AppendFormat(_T("%s"),pCurrAddresses->Description);
         wcout.imbue(locale("CHS")); //设置当前输出字符集
         wcout<<temp.GetBuffer(100)<<endl;

 

四 代码

// Set the flags to pass to GetAdaptersAddresses
ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
// default to unspecified address family (both)
ULONG family = AF_UNSPEC;
LPVOID lpMsgBuf = NULL;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
ULONG outBufLen = 0;
ULONG Iterations = 0;
outBufLen = WORKING_BUFFER_SIZE;
pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen); 
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
if (dwRetVal == ERROR_BUFFER_OVERFLOW) 
{
	FREE(pAddresses);
	pAddresses = NULL;
}			
if (dwRetVal =(GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)) == NO_ERROR)
{				
#ifdef MYDEBUG
	cout<<"GetAdaptersAddresses,no error"<<endl;
#endif
	p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值