获取的网卡MAC地址的通用方法

前一段时间遇到一个问题:我用NetBIOS函数获取机器的MAC地址,机器如果本地网通的时候能够获取得到MAC地址,而我将本地网络断开后,居然就获取不到了,而且我机器上还装有一个无线网卡,我百思不得其解,百度了N下,也没有见谁遇见这样的问题。我就想难道是NetBios函数的问题。后来,我只有另辟蹊径了。看看能否不用该方法来获取,于是在网上搜了一些方法,我认为其中最简单的一种为:

#include "iphlpapi.h"           //  自己动手加上的,我当前用的VC并不提供该头文件
#pragma comment(lib, "iphlpapi.lib")

 

// 获取MAC地址
void CEnrollDlg::GetMacAddress()
{
     IP_ADAPTER_INFO AdapterInfo[16];   // Allocate information for up to 16 NICs
     DWORD dwBufLen = sizeof(AdapterInfo);  // Save the memory size of buffer
 
     DWORD dwStatus = GetAdaptersInfo(   // Call GetAdapterInfo
                                              AdapterInfo,       // [out] buffer to receive data
                                              &dwBufLen);        // [in] size of receive data buffer
     ASSERT(dwStatus == ERROR_SUCCESS);   // Verify return value is valid, no buffer overflow
 
     PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
     if(pAdapterInfo ) // 只获取一个网卡MAC地址

    {
              m_strMacAddress.Format("%02X%02X%02X%02X%02X%02X",
                                                                             pAdapterInfo->Address[0],
                                                                              pAdapterInfo->Address[1],
                                                                              pAdapterInfo->Address[2],
                                                                              pAdapterInfo->Address[3],
                                                                              pAdapterInfo->Address[4],
                                                                              pAdapterInfo->Address[5]);
      }
 }

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页