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. Hook on a HWND

Hook on a HWND

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
7 Posts 3 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.
  • T Offline
    T Offline
    Tony Pottier
    wrote on last edited by
    #1

    Hello, I'm writting a C++ DLL Hook to read any message to any program, so I'm using SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, handleInstance, 0); Then, in my CallWndProc, I need to filter the data coming so I can read the messages from the window I want, but it's not working as expected. During my tests, I wanted to read the messages from the HWND 0x00020afa, so if I put in the CallWndProc:

    LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(nCode < 0)
    return CallNextHookEx(handleHook, nCode, wParam, lParam);

    CWPSTRUCT\* cwpstruct = (CWPSTRUCT\*)lParam;
    
    if((HWND)0x00020afa == cwpstruct->hwnd)
    {
        MessageBox(NULL, "TEST","LOL",MB\_OK);
    }
    
    return CallNextHookEx(handleHook, nCode, wParam, lParam);
    

    }

    I would get the message boxes popping like crazy, but then I decided to add a global HWND to my dll: HWND hWindow; And then a stupid function to set it:

    VOID __stdcall HookHwnd(HWND hWnd)
    {
    hWindow = hWnd;
    }

    So I can choose which HWND i want to track with if(hWindow == cwpstruct->hwnd) instead of if((HWND)0x00020afa == cwpstruct->hwnd) ... But it doesn't work. Am I doing something wrong here? I'm new to the dll world. Also, is this the correct way to read the messages from a window? Thanks!

    M L 2 Replies Last reply
    0
    • T Tony Pottier

      Hello, I'm writting a C++ DLL Hook to read any message to any program, so I'm using SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, handleInstance, 0); Then, in my CallWndProc, I need to filter the data coming so I can read the messages from the window I want, but it's not working as expected. During my tests, I wanted to read the messages from the HWND 0x00020afa, so if I put in the CallWndProc:

      LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if(nCode < 0)
      return CallNextHookEx(handleHook, nCode, wParam, lParam);

      CWPSTRUCT\* cwpstruct = (CWPSTRUCT\*)lParam;
      
      if((HWND)0x00020afa == cwpstruct->hwnd)
      {
          MessageBox(NULL, "TEST","LOL",MB\_OK);
      }
      
      return CallNextHookEx(handleHook, nCode, wParam, lParam);
      

      }

      I would get the message boxes popping like crazy, but then I decided to add a global HWND to my dll: HWND hWindow; And then a stupid function to set it:

      VOID __stdcall HookHwnd(HWND hWnd)
      {
      hWindow = hWnd;
      }

      So I can choose which HWND i want to track with if(hWindow == cwpstruct->hwnd) instead of if((HWND)0x00020afa == cwpstruct->hwnd) ... But it doesn't work. Am I doing something wrong here? I'm new to the dll world. Also, is this the correct way to read the messages from a window? Thanks!

      M Offline
      M Offline
      Malli_S
      wrote on last edited by
      #2

      Hi Tony_P Whenever you install a hook, the DLL gets injected into the process' memory. So, each process will have all together different global variable 'hWindow'. To avoid this you'll have to use the concept of data sharing among the DLLs/Process. Following links may help you. Data Sharing Among DLL/Process[^] Hooking Sample[^]

      -Malli...! :rose:****

      T 1 Reply Last reply
      0
      • T Tony Pottier

        Hello, I'm writting a C++ DLL Hook to read any message to any program, so I'm using SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, handleInstance, 0); Then, in my CallWndProc, I need to filter the data coming so I can read the messages from the window I want, but it's not working as expected. During my tests, I wanted to read the messages from the HWND 0x00020afa, so if I put in the CallWndProc:

        LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
        {
        if(nCode < 0)
        return CallNextHookEx(handleHook, nCode, wParam, lParam);

        CWPSTRUCT\* cwpstruct = (CWPSTRUCT\*)lParam;
        
        if((HWND)0x00020afa == cwpstruct->hwnd)
        {
            MessageBox(NULL, "TEST","LOL",MB\_OK);
        }
        
        return CallNextHookEx(handleHook, nCode, wParam, lParam);
        

        }

        I would get the message boxes popping like crazy, but then I decided to add a global HWND to my dll: HWND hWindow; And then a stupid function to set it:

        VOID __stdcall HookHwnd(HWND hWnd)
        {
        hWindow = hWnd;
        }

        So I can choose which HWND i want to track with if(hWindow == cwpstruct->hwnd) instead of if((HWND)0x00020afa == cwpstruct->hwnd) ... But it doesn't work. Am I doing something wrong here? I'm new to the dll world. Also, is this the correct way to read the messages from a window? Thanks!

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

        Tony_P wrote:

        Am I doing something wrong here?

        Yes. Think of it this way... your DLL is being mapped into multiple applications each of which is executing inside its own Virtual Address Space[^]. This essentially means that each copy of the DLL has a different value for your global variable named hWindow. You need to implement a shared data section: How To Share Data Between Different Mappings of a DLL[^] This will allow all applications to read from the shared data section. Best Wishes, -David Delaune

        T 1 Reply Last reply
        0
        • M Malli_S

          Hi Tony_P Whenever you install a hook, the DLL gets injected into the process' memory. So, each process will have all together different global variable 'hWindow'. To avoid this you'll have to use the concept of data sharing among the DLLs/Process. Following links may help you. Data Sharing Among DLL/Process[^] Hooking Sample[^]

          -Malli...! :rose:****

          T Offline
          T Offline
          Tony Pottier
          wrote on last edited by
          #4

          Even if each process has a different hWindow, wouldn't the function "HookHwnd" I created set every hWindow to the same value? Anyway, thank you for the links, I'm going to try this :)

          L 1 Reply Last reply
          0
          • L Lost User

            Tony_P wrote:

            Am I doing something wrong here?

            Yes. Think of it this way... your DLL is being mapped into multiple applications each of which is executing inside its own Virtual Address Space[^]. This essentially means that each copy of the DLL has a different value for your global variable named hWindow. You need to implement a shared data section: How To Share Data Between Different Mappings of a DLL[^] This will allow all applications to read from the shared data section. Best Wishes, -David Delaune

            T Offline
            T Offline
            Tony Pottier
            wrote on last edited by
            #5

            Well it seems like this shared memory is the solution. I'm going to try it. Thank you!

            1 Reply Last reply
            0
            • T Tony Pottier

              Even if each process has a different hWindow, wouldn't the function "HookHwnd" I created set every hWindow to the same value? Anyway, thank you for the links, I'm going to try this :)

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

              Tony_P wrote:

              wouldn't the function "HookHwnd" I created set every hWindow to the same value

              You could actually make it work this way. You use the RegisterWindowMessage Function[^] to create a unique window message and broadcast it using HWND_BROADCAST[^]. Just make sure that your window message is unique and its not in the WM_USER range so it will not conflict with custom window messages used internally in other applications. RegisterWindowMessage will return something in the range of MAXINTATOM-MAXWORD so you should be safe. In your DLL you could handle this custom message and set the local variable to a target window handle. And since you have both WPARAM and LPARAM this means you could target both PID and window handle. Essentially instructing the DLL "Process with PID x please monitor window n". This is a sort of hackish way of doing it in my opinion, the shared-data segment in my earlier post would probably be a better choice. You could instruct each DLL there also using a similar technique. The great thing about engineering software is there is always many paths to reach the goal. Best Wishes, -David Delaune

              T 1 Reply Last reply
              0
              • L Lost User

                Tony_P wrote:

                wouldn't the function "HookHwnd" I created set every hWindow to the same value

                You could actually make it work this way. You use the RegisterWindowMessage Function[^] to create a unique window message and broadcast it using HWND_BROADCAST[^]. Just make sure that your window message is unique and its not in the WM_USER range so it will not conflict with custom window messages used internally in other applications. RegisterWindowMessage will return something in the range of MAXINTATOM-MAXWORD so you should be safe. In your DLL you could handle this custom message and set the local variable to a target window handle. And since you have both WPARAM and LPARAM this means you could target both PID and window handle. Essentially instructing the DLL "Process with PID x please monitor window n". This is a sort of hackish way of doing it in my opinion, the shared-data segment in my earlier post would probably be a better choice. You could instruct each DLL there also using a similar technique. The great thing about engineering software is there is always many paths to reach the goal. Best Wishes, -David Delaune

                T Offline
                T Offline
                Tony Pottier
                wrote on last edited by
                #7

                Wow! Thank you for sharing all this valuable information to me. Now back on Visual C++ :p

                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