gpt4 book ai didi

c - SetWindowsHookEx 用法的区别

转载 作者:太空狗 更新时间:2023-10-29 15:32:02 25 4
gpt4 key购买 nike

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

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

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

非工作代码:

#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.

您的非工作代码在不运行消息循环的线程上安装钩子(Hook)。

关于c - SetWindowsHookEx 用法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com