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. Windows CBT Hooks

Windows CBT Hooks

Scheduled Pinned Locked Moved C / C++ / MFC
comjsonquestion
12 Posts 3 Posters 1 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.
  • L Lost User

    The remarks section certainly implies quite strongly that the application that calls SetWindowsHookEx must keep running (and pumping messages).

    Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #3

    Thank you Richard. I reread the remarks and I see that you are correct.

    Quote:

    However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.

    When I install the hook, I must pass a pointer to the hook callback function. However, that's a pointer that's only valid inside the process space of the application installing the hook. How does it use that pointer to call the correct code inside the address space of the application that is a target of the hook? :confused:

    The difficult we do right away... ...the impossible takes slightly longer.

    L 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      Thank you Richard. I reread the remarks and I see that you are correct.

      Quote:

      However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.

      When I install the hook, I must pass a pointer to the hook callback function. However, that's a pointer that's only valid inside the process space of the application installing the hook. How does it use that pointer to call the correct code inside the address space of the application that is a target of the hook? :confused:

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Richard Andrew x64 wrote:

      a pointer to the hook callback function.

      The system keeps that address (which is the target of the hook) and uses it to call back into your application at the appropriate time (i.e when the relevant hook event triggers). But if you close your application, its address space is destroyed and the hook is no longer valid, so the system will no longer call it.

      Richard Andrew x64R 1 Reply Last reply
      0
      • L Lost User

        Richard Andrew x64 wrote:

        a pointer to the hook callback function.

        The system keeps that address (which is the target of the hook) and uses it to call back into your application at the appropriate time (i.e when the relevant hook event triggers). But if you close your application, its address space is destroyed and the hook is no longer valid, so the system will no longer call it.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #5

        Thank you for your response. OK, so if the system calls back into my application at the appropriate time, why must the system load the DLL containing the filter function into each process that is hooked? IOW, if the hook function is run inside the installing application, why must the hook function DLL be injected into every targeted application?

        The difficult we do right away... ...the impossible takes slightly longer.

        L L 2 Replies Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Thank you for your response. OK, so if the system calls back into my application at the appropriate time, why must the system load the DLL containing the filter function into each process that is hooked? IOW, if the hook function is run inside the installing application, why must the hook function DLL be injected into every targeted application?

          The difficult we do right away... ...the impossible takes slightly longer.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Sorry I don't know the answer to that one. There is an implication that injecting the hook into another process can be done of the call-back function is in a dll. If that is the case then the dll must be associated withe the address space of that process. I must admit it is a long time since I used this feature so my recollection of it is not 100%.

          Richard Andrew x64R 1 Reply Last reply
          0
          • L Lost User

            Sorry I don't know the answer to that one. There is an implication that injecting the hook into another process can be done of the call-back function is in a dll. If that is the case then the dll must be associated withe the address space of that process. I must admit it is a long time since I used this feature so my recollection of it is not 100%.

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #7

            OK Thank you for your contributions thus far.

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Thank you for your response. OK, so if the system calls back into my application at the appropriate time, why must the system load the DLL containing the filter function into each process that is hooked? IOW, if the hook function is run inside the installing application, why must the hook function DLL be injected into every targeted application?

              The difficult we do right away... ...the impossible takes slightly longer.

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #8

              You need an instance handle to load the DLL ... guess what happens to the instance handle when the application terminates :-)

              In vino veritas

              Richard Andrew x64R 1 Reply Last reply
              0
              • L leon de boer

                You need an instance handle to load the DLL ... guess what happens to the instance handle when the application terminates :-)

                In vino veritas

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #9

                Yes, thank you. I progressed to the point where I'm creating the hook and processing it successfully. But now the problem is that when the hook installer application terminates, it crashes all of the hooked applications *even though* I call UnhookWindowsHookEx() to remove the hook before terminating. Would you have any hints what I can do about that?

                The difficult we do right away... ...the impossible takes slightly longer.

                L 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  Yes, thank you. I progressed to the point where I'm creating the hook and processing it successfully. But now the problem is that when the hook installer application terminates, it crashes all of the hooked applications *even though* I call UnhookWindowsHookEx() to remove the hook before terminating. Would you have any hints what I can do about that?

                  The difficult we do right away... ...the impossible takes slightly longer.

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #10

                  The unhook is usually just done in WM_DESTROY of the main application. From memory it must be before you post WM_QUIT which will kill the instance handle.

                  In vino veritas

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • L leon de boer

                    The unhook is usually just done in WM_DESTROY of the main application. From memory it must be before you post WM_QUIT which will kill the instance handle.

                    In vino veritas

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #11

                    Do you mean that the system waits until the window receives the WM_DESTROY message before it actually unhooks the hook? Or do you mean that I must call UnhookWindowsHookEx BEFORE the WM_QUIT message is posted?

                    The difficult we do right away... ...the impossible takes slightly longer.

                    L 1 Reply Last reply
                    0
                    • Richard Andrew x64R Richard Andrew x64

                      Do you mean that the system waits until the window receives the WM_DESTROY message before it actually unhooks the hook? Or do you mean that I must call UnhookWindowsHookEx BEFORE the WM_QUIT message is posted?

                      The difficult we do right away... ...the impossible takes slightly longer.

                      L Offline
                      L Offline
                      leon de boer
                      wrote on last edited by
                      #12

                      Unhook it before you post the WM_QUIT message

                      In vino veritas

                      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