c++multithreadingwinapithread-safetycreatewindow

How to correctly destroy window that is running from another thread?


The goal that I am trying to achieve is to listen session notifications about lock, unlock and so on. I have to do it in another thread due to architecture and avoiding blocking main thread. Here is what I am doing:


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_WTSSESSION_CHANGE:
        switch (wParam) {
        case WTS_CONSOLE_CONNECT:
        case WTS_SESSION_LOGON:
        case WTS_REMOTE_CONNECT:
        case WTS_SESSION_UNLOCK:
        case WTS_CONSOLE_DISCONNECT:
        case WTS_REMOTE_DISCONNECT:
        case WTS_SESSION_LOGOFF:
        case WTS_SESSION_LOCK:
        case WTS_SESSION_REMOTE_CONTROL:
        case WTS_SESSION_CREATE:
        case WTS_SESSION_TERMINATE:

            break;
        default:
            break;
        }
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

void startListeningNotifications()
{
    const wchar_t g_szClassName[] = L"myWindowClass";

    WNDCLASSEX wc = {};
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = g_szClassName;
    wc.cbSize = sizeof(WNDCLASSEX);

    if (!RegisterClassEx(&wc))
    {
        return;
    }

    HWND hwnd = CreateWindowEx(NULL, g_szClassName, L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
    if (hwnd == NULL)
    {
        return;
    }

    if (!WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_ALL_SESSIONS))
    {
        return;
    }

    ShowWindow(hwnd, SW_HIDE);

    MSG Msg = {};
    while (GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
}

int main()
{
    std::thread listener(startListeningNotifications);
    listener.join();

    return 0;
}

The problem that I am facing right now is that I do not know how to correctly write stopListeningNotifications() method. I have to somehow destroy my window and quit message loop. Please advice how to do that safely. Thanks in advance.


Solution

  • Provided WndProc function handles WM_CLOSE and WM_DESTROY.

    case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
    case WM_DESTROY:
            PostQuitMessage(0);
            break;
    

    So you could send WM_CLOSE message with hwnd of the window created in the thread:

    PostMessage(hwnd, WM_CLOSE, 0, 0)