WlanRegisterNotification() causes exception in KernelBase.dll when WlanCloseHandle() is called

43 views Asked by At

I use WLAN functions of Windows (see wlanapi.h) for communication with a special measuring device. Every written function from me works without any problems (no thrown exception, no returned error codes).

However after connecting to a WLAN net, I see the following message in output pane of Visual Studio:

Exception thrown at 0x7733D902 (KernelBase.dll) in ConsoleApplication3.exe: 0x0000000D: The data is invalid.

Sometimes following message appears:

Exception thrown bei 0x7733D902 (KernelBase.dll) in ConsoleApplication3.exe: 0x0000071A: The remote procedure call was canceled, or if a call time-out was specified, the call timed out.

These messages appears in debug and release mode.

After some research I found the problem origin at WlanRegisterNotification(). I can reproduce the error with the following code:

#include <Windows.h>
#include <wlanapi.h>
#include <iostream>
#include <format>


// ******************** Debug code for reproducing exception in KernelBase.dll ********************

#pragma comment(lib, "Wlanapi.lib")

static
void WINAPI WlanNotificationCallback(PWLAN_NOTIFICATION_DATA Data, PVOID)
{
    if (Data->NotificationSource & WLAN_NOTIFICATION_SOURCE_ACM)
    {
        switch (Data->NotificationCode)
        {
        case wlan_notification_acm_connection_start:
            std::cout << "WlanNotificationCallback>> Connection establishment has started.\n";
            break;
        case wlan_notification_acm_connection_complete:
            std::cout << "WlanNotificationCallback>> Connection established.\n";
            break;
        }
    }
}

void WLAN_Check()
{
    HANDLE ClientHandle = nullptr;
    DWORD CurVersion = 0;
    DWORD q = 0;

    q = WlanOpenHandle(WLAN_API_VERSION, nullptr, &CurVersion, &ClientHandle);
    if (q != ERROR_SUCCESS)
    {
        std::cout << std::format("WlanOpenHandle failed with error: {}\n", q);
        return;
    }
    q = WlanRegisterNotification(ClientHandle, WLAN_NOTIFICATION_SOURCE_ACM, FALSE, WlanNotificationCallback, nullptr, nullptr, nullptr);
    if (q != ERROR_SUCCESS)
    {
        std::cout << std::format("WlanRegisterNotification failed with error: {}\n", q);
        return;
    }
    q = WlanRegisterNotification(ClientHandle, WLAN_NOTIFICATION_SOURCE_ACM, FALSE, WlanNotificationCallback, nullptr, nullptr, nullptr);
    if (q != ERROR_SUCCESS) // Set Breakpoint here
    {
        std::cout << std::format("WlanRegisterNotification failed with error: {}\n", q);
        return;
    }
    q = WlanCloseHandle(ClientHandle, nullptr);
    if (q != ERROR_SUCCESS)
    {
        std::cout << std::format("WlanCloseHandle failed with error: {}\n", q);
        return;
    }
}

int main()
{
    WLAN_Check();
    return 0;
}

If I set breakpoint to last WlanRegisterNotification() and do one debugging step, above message appear.

Is there a bug in my code?

0

There are 0 answers