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. CBTProc not working

CBTProc not working

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingtutorial
3 Posts 2 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.
  • S Offline
    S Offline
    S van Leent
    wrote on last edited by
    #1

    I'm experimenting with hooking, but when I do the following:

    hWndsHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, NULL, NULL);

    I expect it to run:

    LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if (nCode == HCBT_CREATEWND)
    {
    HWND hWnd = reinterpret_cast<HWND>(wParam);
    LPCBT_CREATEWND lpcw = (LPCBT_CREATEWND)lParam;
    LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams;
    ::SetWindowLong(hWnd, GWL_USERDATA, lpCreateParams);
    }

    return CallNextHookEx(hWndsHook, nCode, wParam, lParam);
    

    }

    But it doesn't. There are no compile warnings or whatsoever. I tried to debug it, but when marking it with a breakpoint (at CallNextHookEx) for example, it doesn't break, and it sure doesn't do what I want it to do. And I really am creating windows with CreateWindow, which does do what it needs to do (The MessageLoops work). LPCTSTR Dutch = TEXT("Double Dutch :-)");

    E 1 Reply Last reply
    0
    • S S van Leent

      I'm experimenting with hooking, but when I do the following:

      hWndsHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, NULL, NULL);

      I expect it to run:

      LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if (nCode == HCBT_CREATEWND)
      {
      HWND hWnd = reinterpret_cast<HWND>(wParam);
      LPCBT_CREATEWND lpcw = (LPCBT_CREATEWND)lParam;
      LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams;
      ::SetWindowLong(hWnd, GWL_USERDATA, lpCreateParams);
      }

      return CallNextHookEx(hWndsHook, nCode, wParam, lParam);
      

      }

      But it doesn't. There are no compile warnings or whatsoever. I tried to debug it, but when marking it with a breakpoint (at CallNextHookEx) for example, it doesn't break, and it sure doesn't do what I want it to do. And I really am creating windows with CreateWindow, which does do what it needs to do (The MessageLoops work). LPCTSTR Dutch = TEXT("Double Dutch :-)");

      E Offline
      E Offline
      Ernest Laurentin
      wrote on last edited by
      #2

      Did you check the return value of SetWindowsHookEx? If you specify a thread id NULL, you handle must be valid, and it must be inside of a DLL. If you want to hook your own application, than you must call GetCurrentThreadId(). By the way, hook are pretty difficult to debug. That's the reason for WH_DEBUG! Hope that helps, Good luck! ÿFor the bread of God is he who comes down from heaven and gives life to the world. - John 6:33

      S 1 Reply Last reply
      0
      • E Ernest Laurentin

        Did you check the return value of SetWindowsHookEx? If you specify a thread id NULL, you handle must be valid, and it must be inside of a DLL. If you want to hook your own application, than you must call GetCurrentThreadId(). By the way, hook are pretty difficult to debug. That's the reason for WH_DEBUG! Hope that helps, Good luck! ÿFor the bread of God is he who comes down from heaven and gives life to the world. - John 6:33

        S Offline
        S Offline
        S van Leent
        wrote on last edited by
        #3

        Well, when I wrote the thing and uploaded it, in a minute I knew the solution, It works great. The function looks like:

        static LRESULT CALLBACK CBTProc(int nCode,
        								WPARAM wParam,
        								LPARAM lParam)
        {
        	if (nCode == HCBT\_CREATEWND)
        	{
        		LONG counter = 0;
        		HWND hWnd = reinterpret\_cast(wParam);
        		LPCBT\_CREATEWND lpcw = (LPCBT\_CREATEWND)lParam;
        		LONG lpCreateParams = (LONG)lpcw->lpcs->lpCreateParams;
        
        		// This makes it possible to use "API-style" windows too, where
        		// the lpCreateParams does not point to a class
        
        		if (lpCreateParams != 0 && lpcw->lpcs->hInstance == \_\_hInstance\_\_)
        		{
        			for (iIterator = arrUnboxedClasses.begin(); iIterator != arrUnboxedClasses.end(); iIterator++, counter++)
        			{
        				if (arrUnboxedClasses.at(counter) == lpCreateParams)
        				{
        					::SetWindowLong(hWnd, GWL\_USERDATA, lpCreateParams);
        					goto end;
        				}
        			}
        			
        		}
        	}
        
        	end:
        
        	return CallNextHookEx(\_\_hWndsHook\_\_, nCode, wParam, lParam);
        }
        

        I still think that another application may not crash because I install a hook, this is not a really good design, I think. LPCTSTR Dutch = TEXT("Double Dutch :-)");

        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