Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with Hooks

Problem with Hooks

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bilal78
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • B bilal78

      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

      M Offline
      M Offline
      Mike Beckerleg
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • M Mike Beckerleg

        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.

        B Offline
        B Offline
        bilal78
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • B bilal78

          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

          M Offline
          M Offline
          Mike Beckerleg
          wrote on last edited by
          #4

          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. Mike

          B 1 Reply Last reply
          0
          • M Mike Beckerleg

            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. Mike

            B Offline
            B Offline
            bilal78
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • B bilal78

              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

              M Offline
              M Offline
              Mike Beckerleg
              wrote on last edited by
              #6

              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.

              B 1 Reply Last reply
              0
              • M Mike Beckerleg

                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.

                B Offline
                B Offline
                bilal78
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups