我想从静态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 wlanDataPVOID 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连接/断开连接)后,该解决方案导致应用程序崩溃。

07-24 19:51