life_warrior (life_warrior) wrote in cpp,
life_warrior
life_warrior
cpp

Problems with global mouse hook

Here is the surce code:

DllClient.cpp

HHOOK hook = installHook();

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
...


MainDLL.cpp
static HMODULE hInstance = NULL;
static
HHOOK hook = 0;
TESTDLL_API DWORD threadId = 0;
static
std::ofstream ofs( "datout.txt" );

BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved )
{

switch
(ul_reason_for_call)
{

case
DLL_PROCESS_ATTACH:
hInstance = hModule;
break
;
case
DLL_THREAD_ATTACH:
case
DLL_THREAD_DETACH:
case
DLL_PROCESS_DETACH:
break
;
}

return
TRUE;
}



TESTDLL_API LRESULT CALLBACK mouseProc( int nCode, WPARAM wParam, LPARAM lParam )
{

MOUSEHOOKSTRUCT* data = (MOUSEHOOKSTRUCT*)lParam;
LPARAM lParam2 = MAKELPARAM( data->pt.x, data->pt.y );
ofs << "Point( " << data->pt.x << ", " << data->pt.y << ")" << std::endl;
ofs.flush();
if
( nCode < 0 )
return
CallNextHookEx( 0, nCode, wParam, lParam );
if
( threadId )
{

if
( !PostThreadMessage( threadId, WM_MOUSEMOVE, data->dwExtraInfo, lParam2 ) )
MessageBox( NULL, L"PostThreadMessage failed\n", L"", MB_OK );
}

return
CallNextHookEx( 0, nCode, wParam, lParam );
}



TESTDLL_API HHOOK installHook( void )
{

return
( hook = SetWindowsHookEx( WH_MOUSE, (HOOKPROC)mouseProc, hInstance, 0 ) );
}



TESTDLL_API void uninstallHook( void )
{

UnhookWindowsHookEx( hook );
}

Now, I was expecting to have mouseProc be called each time mouse movement is registered. However, i see that mouseProc from the DLL is called only is cases mouse moves over the main windows (  defined in DllClient.cpp ). Could you please explain, what is the problem and how to make the hook really global? Thank you!
  • Post a new comment

    Error

    default userpic
  • 6 comments