Mouse hook
-
Hello, I have an app installing a mouse hook: The hook setup function...
BOOL CXCapture::Setup()
{
DFUNC_DEF(CXCapture::Setup);
BOOL bRetVal;
//!CODE_START--->
m_hMouse = ::SetWindowsHookEx(WH_MOUSE, &MouseProc, ::AfxGetInstanceHandle(), NULL);
bRetVal = m_hMouse != NULL;
DFUNC_RET(bRetVal != FALSE, DSTR("Could not initialise mouse_hook (Err#%d)", GetLastError()));
return bRetVal;
}The callback function...
LRESULT CALLBACK CXCapture::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
DFUNC_DEF(CXCapture::MouseProc);
//!CODE_START--->
DTRACE(DSTR("Hook: %d::%d, %d", nCode, wParam, lParam));
// now lets allow the message to be processed
return ::CallNextHookEx(m_hMouse, nCode, wParam, lParam);
}It happens that while the mouse is inside my application's (only) dialog box, the callback function (
MouseProc
) is called flawlessly, but when the mouse focus is out of the window, the OS simply unhooks it. Therefore, whenever the mouse leaves the app's window focus, I am left out with no mouse hook whatsoever!! My question is why this is happening and what should (or can) I do in order to solve this. All feedback is greatly appreciated. David Nimrod -
Hello, I have an app installing a mouse hook: The hook setup function...
BOOL CXCapture::Setup()
{
DFUNC_DEF(CXCapture::Setup);
BOOL bRetVal;
//!CODE_START--->
m_hMouse = ::SetWindowsHookEx(WH_MOUSE, &MouseProc, ::AfxGetInstanceHandle(), NULL);
bRetVal = m_hMouse != NULL;
DFUNC_RET(bRetVal != FALSE, DSTR("Could not initialise mouse_hook (Err#%d)", GetLastError()));
return bRetVal;
}The callback function...
LRESULT CALLBACK CXCapture::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
DFUNC_DEF(CXCapture::MouseProc);
//!CODE_START--->
DTRACE(DSTR("Hook: %d::%d, %d", nCode, wParam, lParam));
// now lets allow the message to be processed
return ::CallNextHookEx(m_hMouse, nCode, wParam, lParam);
}It happens that while the mouse is inside my application's (only) dialog box, the callback function (
MouseProc
) is called flawlessly, but when the mouse focus is out of the window, the OS simply unhooks it. Therefore, whenever the mouse leaves the app's window focus, I am left out with no mouse hook whatsoever!! My question is why this is happening and what should (or can) I do in order to solve this. All feedback is greatly appreciated. David NimrodThe Callback function needs to reside in a DLL. the each process can load the dll into it's own address space. Carl:suss:
-
The Callback function needs to reside in a DLL. the each process can load the dll into it's own address space. Carl:suss:
Yeah, I've read about that just an hour ago or so and am modifying the code. Thanks for the reply. David