You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台下使用C++获取BSSID(接入点MAC地址)的方法咨询

Windows平台下C++获取BSSID的可行方法

在Windows平台上,最可靠的实现方式是使用原生WLAN API来获取接入点的BSSID,以下是具体实现方案:

核心实现流程

  1. 初始化WLAN服务并获取操作句柄
  2. 枚举系统中的无线网卡接口
  3. 针对每个接口扫描可用无线接入点
  4. 从扫描结果中提取BSSID并格式化输出

代码示例

编译时需链接wlanapi.lib库,可通过#pragma comment(lib, "wlanapi.lib")添加依赖:

#include <windows.h>
#include <wlanapi.h>
#include <iostream>
#include <iomanip>

#pragma comment(lib, "wlanapi.lib")

// 将BSSID字节数组格式化为XX:XX:XX:XX:XX:XX形式
void PrintBSSID(const unsigned char bssid[6]) {
    std::cout << std::hex << std::setfill('0');
    for (int i = 0; i < 6; ++i) {
        std::cout << std::setw(2) << static_cast<int>(bssid[i]);
        if (i != 5) std::cout << ":";
    }
    std::cout << std::dec << std::setfill(' ');
}

int main() {
    DWORD dwError = 0;
    HANDLE hWlan = NULL;
    DWORD dwNegotiatedVersion = 0;

    // 打开WLAN服务
    dwError = WlanOpenHandle(WLAN_API_VERSION_2_0, NULL, &dwNegotiatedVersion, &hWlan);
    if (dwError != ERROR_SUCCESS) {
        std::cerr << "WlanOpenHandle调用失败: " << dwError << std::endl;
        return 1;
    }

    // 获取无线接口列表
    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    dwError = WlanEnumInterfaces(hWlan, NULL, &pIfList);
    if (dwError != ERROR_SUCCESS) {
        std::cerr << "WlanEnumInterfaces调用失败: " << dwError << std::endl;
        WlanCloseHandle(hWlan, NULL);
        return 1;
    }

    // 遍历每个无线接口
    for (DWORD i = 0; i < pIfList->dwNumberOfItems; ++i) {
        GUID interfaceGuid = pIfList->InterfaceInfo[i].InterfaceGuid;
        std::wcout << L"接口描述: " << pIfList->InterfaceInfo[i].strInterfaceDescription << std::endl;

        // 扫描该接口的可用网络
        dwError = WlanScan(hWlan, &interfaceGuid, NULL, NULL, NULL);
        if (dwError != ERROR_SUCCESS && dwError != ERROR_ALREADY_EXISTS) {
            std::cerr << "WlanScan调用失败: " << dwError << std::endl;
            continue;
        }

        // 获取扫描结果
        PWLAN_BSS_LIST pBssList = NULL;
        dwError = WlanGetNetworkBssList(hWlan, &interfaceGuid, NULL, dot11_BSS_type_infrastructure, FALSE, NULL, &pBssList);
        if (dwError != ERROR_SUCCESS) {
            std::cerr << "WlanGetNetworkBssList调用失败: " << dwError << std::endl;
            continue;
        }

        // 遍历输出每个接入点的BSSID
        std::cout << "发现的接入点信息:" << std::endl;
        for (DWORD j = 0; j < pBssList->dwNumberOfItems; ++j) {
            WLAN_BSS_ENTRY& bssEntry = pBssList->wlanBssEntries[j];
            std::cout << "- BSSID: ";
            PrintBSSID(bssEntry.dot11Bssid);
            std::wcout << L" | SSID: " << bssEntry.dot11Ssid.ucSSID << std::endl;
        }

        // 释放扫描结果内存
        if (pBssList) {
            WlanFreeMemory(pBssList);
            pBssList = NULL;
        }
        std::cout << std::endl;
    }

    // 释放接口列表内存
    if (pIfList) {
        WlanFreeMemory(pIfList);
        pIfList = NULL;
    }

    // 关闭WLAN句柄
    WlanCloseHandle(hWlan, NULL);

    return 0;
}

注意事项

  • 程序需要以管理员权限运行,否则可能无法完成扫描操作
  • Windows Vista及以上版本均支持该WLAN API
  • 代码已包含内存释放逻辑,可避免内存泄漏

此外,也可以通过调用netsh wlan show networks mode=bssid命令并解析输出来获取BSSID,但这种方式依赖命令行输出格式,稳定性远低于原生API,不推荐用于生产环境。

内容的提问来源于stack exchange,提问作者ANJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:45:50