1 year ago

#54227

test-img

Nikolai_Tour

Failed to intercept WM_LBUTTONUP message with WH_GETMESSAGE or WH_MOUSE_LL

In development of GUI automation service, our team faces with the problem that we can not handle WM_LBUTTONUP message correct.

The goal is to intercept WM_LBUTTONUP, disable active window, do our job and show MessageBox with YES and NO buttons:

  1. If clicked Yes, we do our things manually;
  2. Else if clicked NO, do not do things, but in both cases we must prevent clicked window to receive this message and then enable freezed window.

Tried to use WH_MOUSE_LL hook, but clicked window receives click before we can do anything. Tried to use WH_GETMESSAGE hook, we do everything, but independently on user answer the disabled window after enabling receives WM_LBUTTONUP.

The code for messages loop (in Service.exe):

int Loop()
{
    do
    {
        if (PeekMessage(&MessagesQueue, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&MessagesQueue);
            DispatchMessage(&MessagesQueue);
        }
    } while (true);
    ResetHook();
    return (int)MessagesQueue.wParam;
}

The code used with WH_MOUSE_LL (in Service.dll):

LRESULT __stdcall HookProc(int code, WPARAM wParam, LPARAM lParam)
{
    if (wParam == WM_LBUTTONUP && GetAsyncKeyState(VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1))
    {
        HWND w = GetForegroundWindow();
        EnableWindow(w, FALSE);
        // Do things right.
        EnableWindow(w, TRUE);
        SetForegroundWindow(w);
    }
    return CallNextHookEx(MouseHook, code, wParam, lParam);
}

HRESULT SetHook()
{
    MouseHook = SetWindowsHookExW(WH_MOUSE_LL, HookProc, hDllModule, 0);
    if (MouseHook == NULL)
    {
        wcout << L"MouseHook = NULL" << endl;
    }
    else
    {
        wcout << L"MouseHook is" << endl;
    }
    return (MouseHook == NULL) ? ::GetLastError() : NO_ERROR;
}

void ResetHook()
{
    UnhookWindowsHookEx(MouseHook);
}

And the code used with WH_GETMESSAGE (in Service.dll):

LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSG* msg = (MSG*)lParam;
    if (msg->message == WM_LBUTTONUP && GetAsyncKeyState(VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1))
    {
        HWND w = GetForegroundWindow();
        EnableWindow(w, FALSE);
        // Do things right.
        EnableWindow(w, TRUE);
        SetForegroundWindow(w);
    }
}

So my question is how to intercept WM_LBUTTONUP message and prevent the target window to receive this message?

windows-messages

setwindowshookex

mouse-hook

peekmessage

0 Answers

Your Answer

Accepted video resources