none
SetWindowsHookEx doesn't honour LowLevelHooksTimeout registry RRS feed

  • Question

  • We use SetWindowsHookEx (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa) API to hook the mouse events.

    In LowLevelMouseProc callback function, we process the WM_LBUTTONDOWN and WM_LBUTTONUP events.

    While processing those events, it gets timed out in 2 seconds always.

    LowLevelHooksTimeout value in registry is 30 seconds. But still the event gets timed out in 2 seconds.

    This issue is observed in Windows 10 1709, 1803 and 1809 builds.

    This was working fine on Windows 10 1703 build.


    • Edited by goMobile1 Tuesday, December 18, 2018 12:25 PM
    Tuesday, December 18, 2018 12:24 PM

All replies

  • Hi

    Thank you for posting here.

    Because hooks run in the context of an application, they must match the "bitness" of the application.If a 32-bit application installs a global hook on 64-bit Windows, the 32-bit hook is injected into each 32-bit process (the usual security boundaries apply). In a 64-bit process, the threads are still marked as "hooked." However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.

    The mouse input comes from a call to mouse_event, the input was "injected". However, the WH_MOUSE_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.Therefore, the thread that installed the hook must have a message loop.

    Have you matched their "bitness"? If you could provide us a sample of your code ,we will offer you more efficient support.

    Best wishes,

    Drake



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 19, 2018 9:10 AM
    Moderator
  • Hi Drake,
    Thanks for the response.
    The bitness is matched (all 64-bit in our case) and message loop is incorporated.

    Here is the small code snippet :

    LRESULT __stdcall MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if (wParam == WM_LBUTTONDOWN || wParam == WM_LBUTTONUP) {
    printf("In MouseHookCallback - %x\n", wParam);
    }
    if (nCode >= 0)
    {
    switch (wParam)
    {
    case WM_LBUTTONDOWN:
    cout << "Left Button Down" << endl;
    printf("Before Processing\n");
    Sleep(5000); // 5 seconds
    printf("After Processing\n");
    break;

    case WM_LBUTTONUP:
    cout << "Left Button Up" << endl;
    break;
    }
    }
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);
    }

    void SetHook()
    {
    if (!(mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0)))
    {
    cout << "Failed to install mouse hook!" << endl;
    }
    }

    int WINAPI MsgQue()
    {
    SetHook();
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }

    LowLevelHooksTimeout value in HKEY_CURRENT_USER\Control Panel\Desktop is 30000 (30 seconds)

    Case 1 : Output on Win 10 1703 for a click event (if processing time < 30s)
    In MouseHookCallback - 201
    Left Button Down
    Before Processing
    After Processing
    In MouseHookCallback - 202
    Left Button Up

    Case 2 :
    Output on Win 10 1709, 1803, 1809 for a click event (if processing time < 2s)
    In MouseHookCallback - 201
    Left Button Down
    Before Processing
    After Processing
    In MouseHookCallback - 202
    Left Button Up

    Case 3 :
    Output on Win 10 1709, 1803, 1809 for a click event (if processing time > 2s)
    In MouseHookCallback - 201
    Left Button Down
    Before Processing
    After Processing

    Case 3 is the error scenario, where the WM_LBUTTONDOWN is timedout in 2 seconds, while it is expected to timeout only after 30 seconds.
    Thats why we miss the WM_LBUTTONUP in case 3.

    This is working fine till Windows 10 1703 and isn't working from 1709 or above.
    Thanks for helping out and let me know if you need any more information.
    Thursday, December 20, 2018 7:32 AM
  • @goMobile1,

    I can reproduce this issue from side on 1809. I will try to see if I can reproduce your result on 1703. And I will also try to see if I can get something from my internal channel. This may need some time.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 1, 2019 9:41 AM
  • Thanks Barry. I will await your response.

    Your help is much appreciated.


    Thursday, January 3, 2019 6:19 AM
  • There was an intentional change made here to limit the minimum value for a LowLevelHooksTimeout to 1 second starting in RS3 (RedStone 3).  The documentation has not yet been updated to reflect this change.
    Monday, December 2, 2019 9:53 PM