Problem with Hooks
-
Hi All! I am using SetWindowsHookEx(...) to set WH_CBT hook, but the problem is that it never get called in system context. But only works on my application's main window. Don't know whats wrong? May be i am missing a simple thing that is causing the hook to install as system wide. Here is the piece of code i am using to install the hook //Installs the hook //g_hInstDll is set to hInstance in DllMain()
g_hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)Dll_HookProc,(HMODULE)g_hInstDll,0);
Is there anything specific i need to do in order to install hook system wide? Best Regards, Bilal Anjum -
Hi All! I am using SetWindowsHookEx(...) to set WH_CBT hook, but the problem is that it never get called in system context. But only works on my application's main window. Don't know whats wrong? May be i am missing a simple thing that is causing the hook to install as system wide. Here is the piece of code i am using to install the hook //Installs the hook //g_hInstDll is set to hInstance in DllMain()
g_hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)Dll_HookProc,(HMODULE)g_hInstDll,0);
Is there anything specific i need to do in order to install hook system wide? Best Regards, Bilal AnjumThe call to SetWindowsHookEx looks fine, what does it return when you call it? I assume Dll_HookProc is in a DLL and is of the correct type.
-
The call to SetWindowsHookEx looks fine, what does it return when you call it? I assume Dll_HookProc is in a DLL and is of the correct type.
g_hHook is a proper handle and yes all of this is within the DLL. Here is the piece of code:
//defined as DLL_USE __declspec(dllexport) OR __declspec(dllimport) BOOL DLL_USE WINAPI Dll_HookApp() { g_hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)Dll_HookProc,(HMODULE)g_hInstDll,(DWORD)NULL); if (g_hHook) return TRUE; else return FALSE; } BOOL DLL_USE WINAPI Dll_UnhookApp() { if (g_hHook != NULL) return ::UnhookWindowsHookEx(g_hHook); else return FALSE; } static LRESULT CALLBACK Dll_HookProc(int nCode, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; if(nCode < 0) return CallNextHookEx(g_hHook, nCode, wParam, lParam); //Some operations return lRes; }
Best Regards, Bilal Anjum -
g_hHook is a proper handle and yes all of this is within the DLL. Here is the piece of code:
//defined as DLL_USE __declspec(dllexport) OR __declspec(dllimport) BOOL DLL_USE WINAPI Dll_HookApp() { g_hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)Dll_HookProc,(HMODULE)g_hInstDll,(DWORD)NULL); if (g_hHook) return TRUE; else return FALSE; } BOOL DLL_USE WINAPI Dll_UnhookApp() { if (g_hHook != NULL) return ::UnhookWindowsHookEx(g_hHook); else return FALSE; } static LRESULT CALLBACK Dll_HookProc(int nCode, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; if(nCode < 0) return CallNextHookEx(g_hHook, nCode, wParam, lParam); //Some operations return lRes; }
Best Regards, Bilal AnjumThe only difference I can see between your code and the code I have written is in the definition of the callback function. Mine is defined as:
DLL_USE LRESULT CALLBACK CBTHandler(int nCode, WPARAM wParam, LPARAM lParam) { // do stuff here }
So it is not static and it is marked as exported from the DLL. This could be causing the problems. Mike -
The only difference I can see between your code and the code I have written is in the definition of the callback function. Mine is defined as:
DLL_USE LRESULT CALLBACK CBTHandler(int nCode, WPARAM wParam, LPARAM lParam) { // do stuff here }
So it is not static and it is marked as exported from the DLL. This could be causing the problems. Mikewell static can be or is unnecessary but exporting the Hook proc doesn't make any sense, coz the calling application can only set the hook or remove the hook, hook proc will be used internally, so i think need not to export the hook proc. Anyway thanks a lot for the reply, if you have some suggestion like setting up my project (i am using VC++ 6 may be i am not setting up the project), then it might be helpful. if you can send me the basic code for your hook dll (Only hook setup code), it can solve the issue. Best Regrads, Bilal Anjum
-
well static can be or is unnecessary but exporting the Hook proc doesn't make any sense, coz the calling application can only set the hook or remove the hook, hook proc will be used internally, so i think need not to export the hook proc. Anyway thanks a lot for the reply, if you have some suggestion like setting up my project (i am using VC++ 6 may be i am not setting up the project), then it might be helpful. if you can send me the basic code for your hook dll (Only hook setup code), it can solve the issue. Best Regrads, Bilal Anjum
My basic code is the same as yours apart from the definition of the hook function. The DLL will be loaded into the adderess space of all the other application running on your system, they will call the hook function so exporting it could make a difference. Try it, it will only take a minute to test it.
-
My basic code is the same as yours apart from the definition of the hook function. The DLL will be loaded into the adderess space of all the other application running on your system, they will call the hook function so exporting it could make a difference. Try it, it will only take a minute to test it.
Hi! I have solved my problem, infact there was no problem at all. What i was trying to do was to debug a system wide installed hook in the VC debugger, which is not possible as such, so all the events i got were from the application i was dubugging in context with. So if i run the program (not debug it), it works fine. Thanks a lot for your patience and help. Now the real problem is still there, how to debug my program (system wide hook)? Best Regards, Bilal Anjum