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.