Emit signal from static function issue
-
Then I changed the code to:
dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, NULL, 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: qobject_cast<WirelessConnect*>(pThis)->apConnectionComplete(); qDebug() << "Connected!"; break; case wlan_notification_acm_connection_attempt_fail: qobject_cast<WirelessConnect*>(pThis)->apConnectionFailed(); qDebug() << "Failed!"; break; default: break; } }
By removing
Q_ASSERT(dynamic_cast<WirelessConnect*>(context));
some error fixed. But some errors still exists: -
These errors are with connect:
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, setAPConnection); connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, setAPConnectionFailed);
How to connect static signals/slots? Thanks.
-
I have fixed the compile errors.
Code:
initialized outside of theWirelessConnect
class to make it global:
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
connect(&wirelessConnectObj, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection); connect(&wirelessConnectObj, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
But nothing executes in the slots. It connects to the wireless network, but nothing is shown in GUI any message, nothing.
-
@Cobra91151 Did you check that connect() calls actually succeeded?
Was the callback called?
Also using global variables is bad design. -
@Cobra91151 If connect is not working it prints an error at runtime - is there any?
How to connect depends on your design/architecture.
-
I have fixed the issue:
I set
this
to the function:WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, this, NULL, &dwPrevNotif); void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context) { //context = NULL; 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; } }
Then connect it as not static instance:
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection); connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
So I don't need the static global instance anymore and it works now.
Edited:
After some diagnostics (connecting/disconnecting from Access Points), the solution lead to application crash.
-
When using this:
WirelessConnect *pThis = new WirelessConnect(this); dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, pThis, NULL, &dwPrevNotif); void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context) { Q_ASSERT(dynamic_cast<WirelessConnect*>(context)); switch (wlanData->NotificationCode) { case wlan_notification_acm_connection_complete: qobject_cast<WirelessConnect*>(context)->apConnectionComplete(); break; case wlan_notification_acm_connection_attempt_fail: qobject_cast<WirelessConnect*>(context)->apConnectionFailed(); break; default: break; } }
I get a lot of errors:
Can you describe your solution more properly? Thanks.