SetWindowsHookEx 用法的区别

The Difference between to usages of SetWindowsHookEx

首先:我在 Windows XP - 32 位上使用 Visual Studio 2010。

现在我正在尝试编写一个 DLL,它将使另一个应用程序能够使用低级键盘挂钩。

即使我让它工作了 - 我现在想了解原因。

非工作代码:

#include <Windows.h>
#include <stdio.h>

static HINSTANCE hinst;
static HHOOK kbdHook = NULL;

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    printf(":"); fflush(stdout);
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            hinst = hinstDLL;
            CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);

            kbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinst, 0);
            break;
        case DLL_PROCESS_DETACH:
            UnhookWindowsHookEx(kbdHook);
            break;
        default:
            break;
    }

    return TRUE;
}

工作代码:

#include <Windows.h>
#include <stdio.h>

static HINSTANCE hinst;
static HHOOK kbdHook = NULL;

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    printf(":"); fflush(stdout);
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    MSG msg;

    kbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            hinst = hinstDLL;
            CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
            break;
        case DLL_PROCESS_DETACH:
            UnhookWindowsHookEx(kbdHook);
            break;
        default:
            break;
    }

    return TRUE;
}

唯一的区别是我将 SetWindowsHookEx-调用从 DllMain 移到了 ThreadProc

问题:为什么 这个 有很大不同?

LowLevelKeyboardProc callback function 的文档中对此进行了全部解释:

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

您的非工作代码在线程上安装了钩子,运行 没有消息循环。