当加载它们的系统范围的钩子(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/