GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Dec-2008, 08:21
seaders seaders is offline
New Member
 
Join Date: Jun 2008
Posts: 9
seaders is on a distinguished road

Can't get WH_CBT to work right


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:
CPP / C++ / C 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:
CPP / C++ / C 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
CPP / C++ / C 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.
  #2  
Old 14-Dec-2008, 15:55
seaders seaders is offline
New Member
 
Join Date: Jun 2008
Posts: 9
seaders is on a distinguished road

Re: Can't get WH_CBT to work right


sorry to bump, but is there anyone with an idea as to where I'm going wrong? I'm really stuck with this one.

fyi, for anyone reviewing my code, read wParam for *_wParam and lParam for *_lParam, that was a mistake in my condensing for the post.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Work at home. English proofreader needed. $14.50/hour cascade_soft Member Announcements, Advertisements & Offers 0 05-Jun-2006 01:29
Random access files Krazy_yA C Programming Language 6 07-May-2006 02:44
How do web redirection scripts work? rhino1616 Web Design Forum 9 27-Oct-2003 10:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.