如何用C++检测Windows系统WiFi可用性及开关状态?
Hey there! Let's break down your questions about detecting WiFi status using C++, with a focus on Windows since that's what you asked about specifically.
1. Windows-Specific C++ Methods to Check WiFi Availability & State
The most reliable way to do this on Windows is using the Native WiFi API (part of the Windows SDK). This gives you direct access to WiFi adapter information without parsing messy command-line output. Here's a step-by-step guide with working code:
Prerequisites
- Include the required header:
<wlanapi.h> - Link against the library:
wlanapi.lib(add this to your project's linker settings in Visual Studio or your build system)
Example Code
#include <windows.h> #include <wlanapi.h> #include <iostream> #include <vector> // Auto-link the WLAN API library #pragma comment(lib, "wlanapi.lib") void CheckWiFiStatus() { DWORD apiVersion = 2; // Use version 2 of the Native WiFi API HANDLE wlanHandle = NULL; DWORD errorCode = WlanOpenHandle(apiVersion, NULL, &apiVersion, &wlanHandle); if (errorCode != ERROR_SUCCESS) { std::cerr << "Failed to open WLAN handle: Error code " << errorCode << std::endl; return; } PWLAN_INTERFACE_INFO_LIST interfaceList = NULL; errorCode = WlanEnumInterfaces(wlanHandle, NULL, &interfaceList); if (errorCode != ERROR_SUCCESS) { std::cerr << "Failed to enumerate WLAN interfaces: Error code " << errorCode << std::endl; WlanCloseHandle(wlanHandle, NULL); return; } bool hasWiFiAdapter = false; for (DWORD i = 0; i < interfaceList->dwNumberOfItems; ++i) { PWLAN_INTERFACE_INFO interfaceInfo = &interfaceList->InterfaceInfo[i]; hasWiFiAdapter = true; std::wcout << L"Adapter Description: " << interfaceInfo->strInterfaceDescription << std::endl; std::wcout << L"Current State: "; switch (interfaceInfo->isState) { case wlan_interface_state_connected: std::wcout << L"Connected (WiFi is ON and active)" << std::endl; break; case wlan_interface_state_disconnected: std::wcout << L"Disconnected (WiFi is ON but not linked to a network)" << std::endl; break; case wlan_interface_state_adapter_off: std::wcout << L"Adapter OFF (WiFi is disabled)" << std::endl; break; case wlan_interface_state_up: std::wcout << L"Adapter ON (ready but not connected)" << std::endl; break; default: std::wcout << L"Unknown state (code: " << interfaceInfo->isState << ")" << std::endl; } std::wcout << std::endl; } if (!hasWiFiAdapter) { std::cout << "No WiFi adapters detected on this system." << std::endl; } // Clean up allocated resources if (interfaceList != NULL) { WlanFreeMemory(interfaceList); } WlanCloseHandle(wlanHandle, NULL); } int main() { CheckWiFiStatus(); return 0; }
Key Details
- The
wlan_interface_stateenum clearly defines the adapter's status:wlan_interface_state_adapter_off: WiFi is OFF (disabled)- Any other state (connected/disconnected/up): WiFi is ON (enabled, even if not linked to a network)
- This code handles multiple WiFi adapters, so it works for systems with more than one wireless card.
2. Cross-Platform Context (For Your First Question)
If you need to support OSes beyond Windows:
- Linux: Use the
nl80211library (low-level, reliable) or parse output fromiw/iwconfigcommands (simpler but less robust) - macOS: Use the CoreWLAN framework (requires an Objective-C/C++ bridge)
But for Windows, the Native WiFi API is the gold standard—it's official, well-maintained, and avoids the fragility of parsing command-line output.
内容的提问来源于stack exchange,提问作者shahith
相关产品推荐
相关产品推荐

