dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Can't get WH_CBT to work right

  1. #1
    Join Date
    May 2008
    Posts
    8

    Can't get WH_CBT to work right, globally

    So I've a simple window program that starts up and hooks to a dll, (I'll try only post relevant parts)

    Window and calling the DLL:
    Code:
    typedef void (*registerCall)(void*);
    registerCall registerHooks;
    
    static HINSTANCE hinstLib=NULL;
    set_dll_stuff() {
    	std::string dll_attrib = "theDLL.dll";
    	hinstLib = LoadLibrary(dll_attrib.c_str());
    	error_window(hinstLib == NULL,  "ERROR: unable to load DLL, ", dll_attrib);
    
    	dll_attrib = "registerHooks";
    	registerHooks = (registerCall)GetProcAddress(hinstLib, dll_attrib.c_str());
    	error_window(registerHooks == NULL, "ERROR: unable to find DLL function", dll_attrib);
    
    	registerHooks(NULL);
    }
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    {...}
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {...}
    DLL side:
    Code:
    HINSTANCE _hInst= NULL;
    DWORD hookThreadId = 0;
    HWND display= NULL;
    
    HHOOK llMouseHookHandle;
    HHOOK llCBTHookHandle;
    
    size_t bufSize = 32;
    char* buf = new char[bufSize]
    
    extern "C"BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved) {
    	switch (reason) {
    	case DLL_PROCESS_ATTACH:
    		_hInst = hInst;
    		break;
    	default:
    		break;
    	}
    }
    
    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    	if (nCode < 0)
    		return CallNextHookEx(NULL, nCode, wParam, lParam);
    	
    	MOUSEMOVEPOINT * mmp = (MOUSEMOVEPOINT *)lParam;
    
    	POINT pt;
    	pt.x = mmp->x;
    	pt.y = mmp->y;
    
    	if((wParam) == WM_LBUTTONUP)
    	{
    		if (WindowFromPoint(pt) != NULL && (display = WindowFromPoint(pt))) {
    			strcpy(buf, "");
    			GetClassNameA(display, buf, bufSize);
    			cout << buf << endl;
    			SendMessage(display, WM_NULL, 0, 0);
    		}
    	}
    }
    
    
    LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
    	cout << "anything!!!" << endl;
    	return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    
    __declspec(dllexport) void __stdcall
    registerHooks(void* _extra_info) {
    	llMouseHookHandle = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, _hInst, 0);
    	error_window(llMouseHookHandle == NULL, "llMouseHookHandle");
    	llCBTHookHandle = SetWindowsHookEx(WH_CBT, CBTProc, _hInst, 0);
    	error_window(llCBTHookHandle == NULL, "llCBTHookHandle");
    
    	hookThreadId = GetCurrentThreadId();
    }
    
    __declspec(dllexport) void __stdcall
    unregisterHooks() {
    	int ret_val = UnhookWindowsHookEx(llMouseHookHandle);
    	error_window(ret_val == 0, "Unhook llMouseHookHandle");
    	ret_val = UnhookWindowsHookEx(llCBTHookHandle);
    	error_window(ret_val == 0, "Unhook llCBTHookHandle");
    }
    All of this compiles and runs as expected, barring CBT, obviously. I know this because I can have my app running as a taskbar tray icon only and get a cout of window names to my console.

    From the CBTProc, I get nothing, no matter what I've tried to do, I've not been able to get it to print out unless the mouse is within it's own process, then if I resize, or move, or whatever, it works like I want it to.

    I've read through quite a few forum posts, from here and other places and there are some who think
    Code:
    	llCBTHookHandle = SetWindowsHookEx(WH_CBT, CBTProc, NULL, GetCurrentThreadId());
    or variations of that. From my efforts of trying to get it to work, though, none of them have.

    Am I doing something elementary that is preventing it to work, or is my whole approach wrong? Would I need to go down the route of injecting my DLL into processes (I've not succeeded in this yet, either, but if I need to I'll incorporate that)?

    All I want is to listen for window opening and closing, to get a handle of a particular window and only run when that's open, then if / when that closes, I want my app to close too.

    Thank you all for any help that you could give me.
    Last edited by seaders; December 14th, 2008 at 04:52 PM.

  2. #2
    Join Date
    May 2008
    Posts
    8

    Re: Can't get WH_CBT to work right

    sorry to bump, but is there anyone with an idea where I'm going wrong, everywhere that I've looked at says I'm doing it right, but WH_CBT hook only looks to be working locally, and not globally.

  3. #3
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Re: Can't get WH_CBT to work right

    I stopped reading when I saw this:

    Code:
    std::string dll_attrib = "theDLL.dll";
    registerHooks = (registerCall)GetProcAddress(hinstLib, dll_attrib.c_str());
    there is no function in your lib (at least you didn't post it) with the name "theDLL.dll" obv

    Anyways, your CBT Callback does not look good, have a look over msdn for that.
    call CallNextHookEx, check nCode etc.
    By the way, I'm pretty sure calling cout or printf() within a library as no effect as there is
    no stdout defined. But I might be wrong there.

    Greetings, Andy

  4. #4
    Join Date
    May 2008
    Posts
    8

    Re: Can't get WH_CBT to work right

    aww, CatShoe, you've finally cracked it for me. it wasn't anything about the "thedll.dll" part, that was just another typo, that was the part where the code was looking for the dll, LoadLibrary, rather than GetProcAddress. no, what you've just made me realise was the whole cout part. I was forgetting the whole problem of shared memory and while cout was printing for low level stuff, like WH_MOUSE_LL, it would only print to the proper console when my window was on focus. when I changed the dll to log to a file, it was firing the event globally.

    thank you, thank you, thank you, this has been a headwrecker to me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)