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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetWindowsHookEx from NT Service

SetWindowsHookEx from NT Service

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 4 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.
  • K Offline
    K Offline
    kelt666
    wrote on last edited by
    #1

    Hello, I'm experiencing a trouble while setting a hook procedure form an NT Service. Hook procedure is intended to capture keyboard events (WH_KEYBOARD). Thread, that installs the hook, calls SwitchDesktop to switch to desktop, that receives user input. SetWindowsHookEx returns non-null hook handle, but the hook procedure is not ever called. Are there any prerequisites I'm missing?

    F J 2 Replies Last reply
    0
    • K kelt666

      Hello, I'm experiencing a trouble while setting a hook procedure form an NT Service. Hook procedure is intended to capture keyboard events (WH_KEYBOARD). Thread, that installs the hook, calls SwitchDesktop to switch to desktop, that receives user input. SetWindowsHookEx returns non-null hook handle, but the hook procedure is not ever called. Are there any prerequisites I'm missing?

      F Offline
      F Offline
      FarPointer
      wrote on last edited by
      #2

      Is it a SytemWideHook or ThreadSpecific Hook and were is the hook procedure present. Regards, FarPointer Blog:FARPOINTER

      K 1 Reply Last reply
      0
      • K kelt666

        Hello, I'm experiencing a trouble while setting a hook procedure form an NT Service. Hook procedure is intended to capture keyboard events (WH_KEYBOARD). Thread, that installs the hook, calls SwitchDesktop to switch to desktop, that receives user input. SetWindowsHookEx returns non-null hook handle, but the hook procedure is not ever called. Are there any prerequisites I'm missing?

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        I would start by creating a little app and running it under the debugger.  Make sure you see your hook's DLL getting loaded into the running process.  If not, then the hook is not getting installed correctly.    If it is being loaded into the running process, then perhaps the way the events are being filtered is incorrect?    Also, what desktop are you in when the hook is installed?  Generally, hooks only work in the desktop that the hook is installed in.    Peace! -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        S K 2 Replies Last reply
        0
        • J James R Twine

          I would start by creating a little app and running it under the debugger.  Make sure you see your hook's DLL getting loaded into the running process.  If not, then the hook is not getting installed correctly.    If it is being loaded into the running process, then perhaps the way the events are being filtered is incorrect?    Also, what desktop are you in when the hook is installed?  Generally, hooks only work in the desktop that the hook is installed in.    Peace! -=- James


          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          DeleteFXPFiles & CheckFavorites (Please rate this post!)

          S Offline
          S Offline
          sudeesh
          wrote on last edited by
          #4

          u can specify the ALLOW_OTHER_ACCOUNT_HOOK and make the service interractive to input desktop hello

          J 1 Reply Last reply
          0
          • S sudeesh

            u can specify the ALLOW_OTHER_ACCOUNT_HOOK and make the service interractive to input desktop hello

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            I am not familiar with the ALLOW_OTHER_ACCOUNT_HOOK in the context of hooks, and MSDN is not helping me here.  Where is this value documented?    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            S 1 Reply Last reply
            0
            • J James R Twine

              I would start by creating a little app and running it under the debugger.  Make sure you see your hook's DLL getting loaded into the running process.  If not, then the hook is not getting installed correctly.    If it is being loaded into the running process, then perhaps the way the events are being filtered is incorrect?    Also, what desktop are you in when the hook is installed?  Generally, hooks only work in the desktop that the hook is installed in.    Peace! -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

              K Offline
              K Offline
              kelt666
              wrote on last edited by
              #6

              Thank you for your reply. I've eventually tested the hook in a small console application first. The DLL is getting loaded properly, the hook works ok. Concerning the service, I allow it to interact with desktop and use the following code to switch the desktop before setting hook:

              HDESK hUserDesktop = ::OpenInputDesktop( DF_ALLOWOTHERACCOUNTHOOK, FALSE, GENERIC_ALL );

              ::SwitchDesktop( hUserDesktop );

              HHOOK hHook = ::SetWindowsHookEx( WH_KEYBOARD, prHook, bfInst, 0 );

              All of these function return successful results. I'm aware I'm missing some important stuff to do.

              J 1 Reply Last reply
              0
              • F FarPointer

                Is it a SytemWideHook or ThreadSpecific Hook and were is the hook procedure present. Regards, FarPointer Blog:FARPOINTER

                K Offline
                K Offline
                kelt666
                wrote on last edited by
                #7

                This is a system-wide hook, the hook procedure resides in a DLL. This DLL is loaded by a service, then the hook is installed.

                1 Reply Last reply
                0
                • K kelt666

                  Thank you for your reply. I've eventually tested the hook in a small console application first. The DLL is getting loaded properly, the hook works ok. Concerning the service, I allow it to interact with desktop and use the following code to switch the desktop before setting hook:

                  HDESK hUserDesktop = ::OpenInputDesktop( DF_ALLOWOTHERACCOUNTHOOK, FALSE, GENERIC_ALL );

                  ::SwitchDesktop( hUserDesktop );

                  HHOOK hHook = ::SetWindowsHookEx( WH_KEYBOARD, prHook, bfInst, 0 );

                  All of these function return successful results. I'm aware I'm missing some important stuff to do.

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #8

                  Are you sure the hook is really not getting executed?  You cannot set a breakpoint in the debugger with the service loaded and expect to see it fire from within the service, because the hook is getting loaded into the target processes, and it will fire under the context of those processes.    If you do something like using OutputDebugString(...) to write out a string to the debugger (do not use TRACE/TRACEx(...)).  Using the console application under the debugger, you should be able to see the output from that function if it is being fired.    Peace! -=- James


                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

                  K 1 Reply Last reply
                  0
                  • J James R Twine

                    Are you sure the hook is really not getting executed?  You cannot set a breakpoint in the debugger with the service loaded and expect to see it fire from within the service, because the hook is getting loaded into the target processes, and it will fire under the context of those processes.    If you do something like using OutputDebugString(...) to write out a string to the debugger (do not use TRACE/TRACEx(...)).  Using the console application under the debugger, you should be able to see the output from that function if it is being fired.    Peace! -=- James


                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                    K Offline
                    K Offline
                    kelt666
                    wrote on last edited by
                    #9

                    Thank you for your reply! I use ::OutputDebugStringA() to enable debug output and DbgView from sysinternals to capture this output. This pair works, as I receive debug output during serivce initialization. But still no output from hook.

                    J 1 Reply Last reply
                    0
                    • K kelt666

                      Thank you for your reply! I use ::OutputDebugStringA() to enable debug output and DbgView from sysinternals to capture this output. This pair works, as I receive debug output during serivce initialization. But still no output from hook.

                      J Offline
                      J Offline
                      James R Twine
                      wrote on last edited by
                      #10

                      Sorry - no other idea other than that the hook is just not getting across to the different desktop...    Peace! -=- James


                      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                      DeleteFXPFiles & CheckFavorites (Please rate this post!)

                      K 1 Reply Last reply
                      0
                      • J James R Twine

                        I am not familiar with the ALLOW_OTHER_ACCOUNT_HOOK in the context of hooks, and MSDN is not helping me here.  Where is this value documented?    Peace! -=- James


                        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                        DeleteFXPFiles & CheckFavorites (Please rate this post!)

                        S Offline
                        S Offline
                        sudeesh
                        wrote on last edited by
                        #11

                        http://support.microsoft.com/kb/163892/EN-US/ hello

                        J 1 Reply Last reply
                        0
                        • J James R Twine

                          Sorry - no other idea other than that the hook is just not getting across to the different desktop...    Peace! -=- James


                          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                          DeleteFXPFiles & CheckFavorites (Please rate this post!)

                          K Offline
                          K Offline
                          kelt666
                          wrote on last edited by
                          #12

                          I've got it at last! The problem was with security settings of some of synchronization objects. Being created withing a service, they had their owner set to SYSTEM and none of user code could ever open them (although, this code works if it is run from a test application under my account) Anyway, thank you for your ideas!

                          1 Reply Last reply
                          0
                          • S sudeesh

                            http://support.microsoft.com/kb/163892/EN-US/ hello

                            J Offline
                            J Offline
                            James R Twine
                            wrote on last edited by
                            #13

                            Yes, I now know that DF_ALLOWOTHERACCOUNTHOOK was the identifier you were trying to refer to.    Peace! -=- James


                            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                            DeleteFXPFiles & CheckFavorites (Please rate this post!)

                            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