使用微软例子查询Wifi 当前连接的ssid名及连接状态和信号强度

WlanQueryInterface 函数微软的例子有内存泄漏20190418(以后可能会修补)
https://docs.microsoft.com/zh-cn/windows/desktop/api/wlanapi/nf-wlanapi-wlanqueryinterface
直接使用了微软的例子改名当函数,结果运行几次后失败了。
原因是: WlanOpenHandle(
In DWORD dwClientVersion,
Reserved PVOID pReserved,
Out PDWORD pdwNegotiatedVersion,
Out PHANDLE phClientHandle
);

打开的客户句柄phClientHandle没有关闭。

给出修改后例子代码备忘:

// WlanQueryInterface.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <wlanapi.h>
#include <Windot11.h>           // for DOT11_SSID struct
#include <objbase.h>
#include <wtypes.h>

//#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>

// Need to link with Wlanapi.lib and Ole32.lib
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "ole32.lib")

int wmain()
{

	// Declare and initialize variables.

	HANDLE hClient = NULL;
	DWORD dwMaxClient = 2;      //    
	DWORD dwCurVersion = 0;
	DWORD dwResult = 0;
	DWORD dwRetVal = 0;
	int iRet = 0;

	WCHAR GuidString[39] = { 0 };

	unsigned int i, k;

	// variables used for WlanEnumInterfaces

	PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
	PWLAN_INTERFACE_INFO pIfInfo = NULL;

	// variables used for WlanQueryInterfaces for opcode = wlan_intf_opcode_current_connection
	PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
	DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
	WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;

	dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
	if (dwResult != ERROR_SUCCESS) {
		wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
		if (hClient)
		{
			WlanCloseHandle(hClient, NULL);
		}
		return 1;
		// You can use FormatMessage here to find out why the function failed
	}

	dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
	if (dwResult != ERROR_SUCCESS) {
		wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
		if (hClient)
		{
			WlanCloseHandle(hClient, NULL);
		}
		return 1;
		// You can use FormatMessage here to find out why the function failed
	}
	else {
		wprintf(L"Num Entries: %lu\n", pIfList->dwNumberOfItems);
		wprintf(L"
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是在QT中扫描附近WiFi并打印SSID信号强度的示例代码: ```cpp #include <QCoreApplication> #include <QList> #include <QNetworkInterface> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); foreach (QNetworkInterface interface, interfaces) { QList<QNetworkAddressEntry> entries = interface.addressEntries(); foreach (QNetworkAddressEntry entry, entries) { if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { QNetworkInterface::InterfaceFlags flags = interface.flags(); if (flags.testFlag(QNetworkInterface::IsUp) && !flags.testFlag(QNetworkInterface::IsLoopBack)) { QList<QNetworkConfiguration> configs = QNetworkConfiguration::allConfigurations(); foreach (QNetworkConfiguration config, configs) { if (config.bearerType() == QNetworkConfiguration::BearerWLAN) { QNetworkSession session(config); QList<QNetworkConfiguration> activeConfigs = session.configuration().childConfigurations(); foreach (QNetworkConfiguration activeConfig, activeConfigs) { if (activeConfig.name() == interface.name()) { QProcess process; QString command = "iwlist " + interface.name() + " scanning"; process.start(command); process.waitForFinished(); QString output = process.readAllStandardOutput(); QStringList lines = output.split("\n"); foreach (QString line, lines) { line = line.trimmed(); if (line.startsWith("ESSID:")) { QString ssid = line.mid(7, line.length() - 8); qDebug() << "SSID: " << ssid; } else if (line.startsWith("Signal level=")) { QString signalLevel = line.mid(14, line.length() - 17); qDebug() << "Signal level: " << signalLevel; } } } } } } } } } } return a.exec(); } ``` 该代码使用了`QNetworkInterface`和`QProcess`类来扫描附近的WiFi,并打印出SSID信号强度。需要注意的是,该代码只能在Linux系统中运行,并且需要安装`iwlist`命令行工具。在其他操作系统中,可能需要使用其他方式来扫描WiFi并获取SSID信号强度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值