winapi - 解除全局 CBT Hook 后从所有进程中卸载 DLL

标签 winapi setwindowshookex unhookwindowshookex

当加载它们的系统范围的钩子(Hook)被卸载时,如何从所有进程中正确卸载 DLL?

来自 MSDN :

You can release a global hook procedure by using UnhookWindowsHookEx, but this function does not free the DLL containing the hook procedure. This is because global hook procedures are called in the process context of every application in the desktop, causing an implicit call to the LoadLibrary function for all of those processes. Because a call to the FreeLibrary function cannot be made for another process, there is then no way to free the DLL. The system eventually frees the DLL after all processes explicitly linked to the DLL have either terminated or called FreeLibrary and all processes that called the hook procedure have resumed processing outside the DLL.



所以我正在寻找的是一种检测钩子(Hook)何时脱钩的方法,然后调用 FreeLibrary从所有被 Hook 的进程中。卸载钩子(Hook)时,是否有任何其他方法可以立即卸载 DLL?

最佳答案

Hook dll 在它们的消息循环中被卸载。强制它们通过消息循环有助于卸载它们。

在 UnhookWindowsHookEx 之后添加这个以强制唤醒所有消息循环:

DWORD dwResult;
SendMessageTimeout(HWND_BROADCAST, WM_NULL, 0, 0, SMTO_ABORTIFHUNG|SMTO_NOTIMEOUTIFNOTHUNG, 1000, &dwResult);

但是,我仍然不时遇到问题。我不知道它是从哪里来的。我想一个锁定的进程可能会阻止 dll 卸载,但我没有证据证明这一点。

关于winapi - 解除全局 CBT Hook 后从所有进程中卸载 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3164190/

相关文章:

windows - 检测/ Hook 其他外部进程的窗口移动/拖动

c++ - 为什么 SetWindowsHookEx 不接受钩子(Hook)程序?

delphi - 仅 Hook WH_GETMESSAGE 并过滤 WM_SETTEXT

c++ - 如何确保即使在异常终止时也会调用 UnhookWindowsHookEx?

c++ - 如何限制在 VS 设计器中创建的编辑框中的数字

c++ - 在另一个中挂起和恢复一个 C++ 线程

c++ - 创建具有特定格式的位图以反转/镜像它

c - WinAPI 组合框问题 - 按案例跳过