一 , 两个函数
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