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

Threaded View

  1. #1
    Join Date
    May 2008

    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:
    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);
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    DLL side:
    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) {
    		_hInst = hInst;
    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    	if (nCode < 0)
    		return CallNextHookEx(NULL, nCode, wParam, 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
    	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.

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)