我創建了一個使用CBT掛鉤來掛鉤某些事件的DLL。它似乎只適用於啓動過程中創建的窗口...CBT Hook只接收一些事件
我的系統是Windows 7 x64,但在x32上的行爲也是一樣的。
這是代碼(抱歉,我不是C++專家):
#include "windows.h"
extern "C"
{
static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam);
HINSTANCE g_hDll = NULL;
HWND g_hNotifyWin = NULL;
DWORD g_uNotifyMsg = NULL;
HHOOK g_hHook = NULL;
__declspec(dllexport) HHOOK SetCbtHook(HWND hWnd, LPCWSTR lStrMsg, DWORD threadId)
{
g_hNotifyWin = hWnd;
g_uNotifyMsg = RegisterWindowMessage(lStrMsg);
g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CbtProcCb, g_hDll, threadId);
return g_hHook;
}
__declspec(dllexport) int UnsetCbtHook()
{
if (!g_hHook)
return 0;
return UnhookWindowsHookEx(g_hHook);
}
}
static LRESULT CALLBACK CbtProcCb(int nCode, WPARAM wParam, LPARAM lParam)
{
SendNotifyMessage(g_hNotifyWin, g_uNotifyMsg, nCode, wParam); // Send nCode to check the received event
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
g_hDll = hinstDLL;
return true;
}
任何提示?
感謝您的帖子。我試着用你的代碼(稍微修改一下,因爲我認爲你用g_uNotifyMsg代替了g_hNotifyWin),但是它的行爲是一樣的。 無論如何,我想如果你猜測它是正確的,它也必須對其他全局變量有效,如g_uNotifyMsg。在DllMain中初始化它們可能是正確的嗎? – cyrusza
對不起,低級別的答案。你是對的。更新! – manuell
我設置了DllMain中的所有變量以避免回調中的混亂。它的工作,所以你的猜測是正確的!感謝您的幫助,非常感謝! 您是否認爲有機會通過這些值而不對其進行硬編碼? – cyrusza