我想在静态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时;作为app连接到Access Point后的指针,然后应用程序崩溃。
初始化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
中连接它们。
答案 0 :(得分:1)
我已经解决了这个问题:
我将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);
所以我不再需要静态全局实例了,现在就可以了。
<强>编辑:强>
在某些诊断(连接/断开与接入点之间)后,解决方案会导致应用程序崩溃。