我想从静态wlan
函数注册notificationCallBack
连接时发出信号。
码:
WirelessConnect.h static void WINAPI notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context);
WirelessConnect.cpp
DWORD dwResult = 0;
dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, NULL, NULL, &dwPrevNotif);
void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
context = NULL;
switch (wlanData->NotificationCode) {
case wlan_notification_acm_connection_complete:
emit apConnectionComplete();
qDebug() << "Connected!";
break;
case wlan_notification_acm_connection_attempt_fail:
emit apConnectionFailed();
qDebug() << "Failed!";
break;
default:
break;
}
}
测试.cpp:
WirelessConnect *wirelessAPConnect = new WirelessConnect();
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, setAPConnection);
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, setAPConnectionFailed);
问题是
error: C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers
。那么如何从
static
函数发出信号,例如在QMessageBox
类中显示有关wlan连接的Test
?来自上面链接的解决方案不起作用:
1方法:
notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
函数仅需要2个参数PWLAN_NOTIFICATION_DATA wlanData
和PVOID context
,而不引用该类作为参数2方法:导致错误:连接中的
C3867: 'WirelessConnect::apConnectionComplete': non-standard syntax; use '&' to create a pointer to member
:connect(wirelessAPConnect, WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
我已经用2方法修复了编译错误,但它没有发出任何东西。
代码:
WirelessConnect.cpp
static WirelessConnect wirelessConnectObj;
void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
context = NULL;
switch (wlanData->NotificationCode) {
case wlan_notification_acm_connection_complete:
wirelessConnectObj.apConnectionComplete();
qDebug() << "Connected!";
break;
case wlan_notification_acm_connection_attempt_fail:
wirelessConnectObj.apConnectionFailed();
qDebug() << "Failed!";
break;
default:
break;
}
}
dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, NULL, NULL, &dwPrevNotif);
Test.cpp
WirelessConnect *wirelessAPConnect = new WirelessConnect();
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
没有编译错误,并且
setAPConnection
不执行。我认为
static WirelessConnect wirelessConnectObj;
为NULL,它什么也不执行。当我使用静态WirelessConnect * wirelessConnectObj时;应用程序连接到访问点后,将其作为指针,然后应用程序崩溃。初始化
static WirelessConnect *wirelessConnectObj = new WirelessConnect();
时,该应用程序可以运行,但不执行信号/插槽。编辑:
WirelessConnect
类还管理其他非静态wlan
函数,这些函数连接到信号和插槽。所以我不能删除wirelessAPConnect
对象,而只能使用静态对象。我也将static WirelessConnect wirelessConnectObj;
初始化为Test
并将其连接:connect(&wirelessConnectObj, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(&wirelessConnectObj, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
输出是相同的-什么都没有。我认为,这是因为我创建了2个不同的
WirelessConnect
类静态实例。我只需要WirelessConnect
的一个静态实例,该实例应该是全局的,以发送信号并将它们连接到Test
中。 最佳答案
我已经解决了这个问题:
我将this
设置为该函数:
WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, this, NULL, &dwPrevNotif);
void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
WirelessConnect *pThis = (WirelessConnect*) context;
switch (wlanData->NotificationCode) {
case wlan_notification_acm_connection_complete:
pThis->apConnectionComplete();
qDebug() << "Connected!";
break;
case wlan_notification_acm_connection_attempt_fail:
pThis->apConnectionFailed();
qDebug() << "Failed!";
break;
default:
break;
}
}
然后将其连接为非静态实例:
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
因此,我不再需要静态全局实例,并且现在可以使用了。
编辑:
经过一些诊断(从Access Point连接/断开连接)后,该解决方案导致应用程序崩溃。