CBTProc not working
-
I'm experimenting with hooking, but when I do the following:
hWndsHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, NULL, NULL);
I expect it to run:
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
HWND hWnd = reinterpret_cast<HWND>(wParam);
LPCBT_CREATEWND lpcw = (LPCBT_CREATEWND)lParam;
LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams;
::SetWindowLong(hWnd, GWL_USERDATA, lpCreateParams);
}return CallNextHookEx(hWndsHook, nCode, wParam, lParam);
}
But it doesn't. There are no compile warnings or whatsoever. I tried to debug it, but when marking it with a breakpoint (at CallNextHookEx) for example, it doesn't break, and it sure doesn't do what I want it to do. And I really am creating windows with CreateWindow, which does do what it needs to do (The MessageLoops work). LPCTSTR Dutch = TEXT("Double Dutch :-)");
-
I'm experimenting with hooking, but when I do the following:
hWndsHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, NULL, NULL);
I expect it to run:
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
HWND hWnd = reinterpret_cast<HWND>(wParam);
LPCBT_CREATEWND lpcw = (LPCBT_CREATEWND)lParam;
LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams;
::SetWindowLong(hWnd, GWL_USERDATA, lpCreateParams);
}return CallNextHookEx(hWndsHook, nCode, wParam, lParam);
}
But it doesn't. There are no compile warnings or whatsoever. I tried to debug it, but when marking it with a breakpoint (at CallNextHookEx) for example, it doesn't break, and it sure doesn't do what I want it to do. And I really am creating windows with CreateWindow, which does do what it needs to do (The MessageLoops work). LPCTSTR Dutch = TEXT("Double Dutch :-)");
Did you check the return value of
SetWindowsHookEx
? If you specify a thread id NULL, you handle must be valid, and it must be inside of a DLL. If you want to hook your own application, than you must callGetCurrentThreadId()
. By the way, hook are pretty difficult to debug. That's the reason forWH_DEBUG
! Hope that helps, Good luck! ÿFor the bread of God is he who comes down from heaven and gives life to the world. - John 6:33 -
Did you check the return value of
SetWindowsHookEx
? If you specify a thread id NULL, you handle must be valid, and it must be inside of a DLL. If you want to hook your own application, than you must callGetCurrentThreadId()
. By the way, hook are pretty difficult to debug. That's the reason forWH_DEBUG
! Hope that helps, Good luck! ÿFor the bread of God is he who comes down from heaven and gives life to the world. - John 6:33Well, when I wrote the thing and uploaded it, in a minute I knew the solution, It works great. The function looks like:
static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HCBT\_CREATEWND) { LONG counter = 0; HWND hWnd = reinterpret\_cast(wParam); LPCBT\_CREATEWND lpcw = (LPCBT\_CREATEWND)lParam; LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams; // This makes it possible to use "API-style" windows too, where // the lpCreateParams does not point to a class if (lpCreateParams != 0 && lpcw->lpcs->hInstance == \_\_hInstance\_\_) { for (iIterator = arrUnboxedClasses.begin(); iIterator != arrUnboxedClasses.end(); iIterator++, counter++) { if (arrUnboxedClasses.at(counter) == lpCreateParams) { ::SetWindowLong(hWnd, GWL\_USERDATA, lpCreateParams); goto end; } } } } end: return CallNextHookEx(\_\_hWndsHook\_\_, nCode, wParam, lParam); }
I still think that another application may not crash because I install a hook, this is not a really good design, I think. LPCTSTR Dutch = TEXT("Double Dutch :-)");