hon67
asked on
SetWindowsHookEx(...)
Hi,
How can I monitor all the message (MOUSE and KEYBOARD) of the Windows even though my application is not active?
ie. the user is using another application at that moment but my application is still running.
I have tried this but I can only catch the msg while my application is active.
I am using Visual C++ 5.0 Prof. Edition to test this:
at the exe file:
1.) Link the library file at the Project->Setting->Link
2.) declare the following lines at cpp file
__declspec( dllimport ) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
__declspec( dllimport ) BOOL Hook();
__declspec( dllimport ) BOOL UnSetHook();
3.) At the OnInitDialog Function(),
HINSTANCE hinstDLL;
DWORD errorNumber;
hinstDLL = LoadLibrary((LPCTSTR)"Hook Dll.dll");
if ( hinstDLL == NULL )
{
errorNumber = GetLastError();
}
Hook();
4.) At the OnDestroy Function
UnSetHook();
-------------------------- ---------- ---------- ---------- ---------
At the Dll's cpp file:
#pragma comment(linker, "-section:hookdata,rws")
#pragma data_seg("hookdata")
HHOOK g_hHook = NULL; // Global hook handle
#pragma data_seg()
HINSTANCE g_hinstDll = NULL;
__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
TRACE("CATCH HC_ACTION MESSAGE\n");
else if (code == HC_NOREMOVE)
TRACE("CATCH HC_REMOVE MESSAGE\n");
// Pass through the hook chain
return CallNextHookEx(g_hHook, code, wParam, lParam);
}
__declspec(dllexport) BOOL Hook()
{
if (g_hHook != NULL)
return FALSE;
g_hHook = SetWindowsHookEx(WH_KEYBOA RD, KeyboardProc, g_hinstDll, 0);
return (g_hHook != NULL);
}
__declspec(dllexport) BOOL UnSetHook()
{
BOOL rc;
rc = UnhookWindowsHookEx(g_hHoo k);
if (rc)
g_hHook = NULL;
return rc;
}
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls( hInstance) ;
g_hinstDll = hInstance; // Save DLL instance handle
break;
}
return TRUE;
}
-------------------------- ---------- ---------- ---------
Any mistake I have made?
Until now, I can only catch the msg when I press the keyboard button and my application is being active at the same time.
Thanks for your help!!!
How can I monitor all the message (MOUSE and KEYBOARD) of the Windows even though my application is not active?
ie. the user is using another application at that moment but my application is still running.
I have tried this but I can only catch the msg while my application is active.
I am using Visual C++ 5.0 Prof. Edition to test this:
at the exe file:
1.) Link the library file at the Project->Setting->Link
2.) declare the following lines at cpp file
__declspec( dllimport ) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
__declspec( dllimport ) BOOL Hook();
__declspec( dllimport ) BOOL UnSetHook();
3.) At the OnInitDialog Function(),
HINSTANCE hinstDLL;
DWORD errorNumber;
hinstDLL = LoadLibrary((LPCTSTR)"Hook
if ( hinstDLL == NULL )
{
errorNumber = GetLastError();
}
Hook();
4.) At the OnDestroy Function
UnSetHook();
--------------------------
At the Dll's cpp file:
#pragma comment(linker, "-section:hookdata,rws")
#pragma data_seg("hookdata")
HHOOK g_hHook = NULL; // Global hook handle
#pragma data_seg()
HINSTANCE g_hinstDll = NULL;
__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
TRACE("CATCH HC_ACTION MESSAGE\n");
else if (code == HC_NOREMOVE)
TRACE("CATCH HC_REMOVE MESSAGE\n");
// Pass through the hook chain
return CallNextHookEx(g_hHook, code, wParam, lParam);
}
__declspec(dllexport) BOOL Hook()
{
if (g_hHook != NULL)
return FALSE;
g_hHook = SetWindowsHookEx(WH_KEYBOA
return (g_hHook != NULL);
}
__declspec(dllexport) BOOL UnSetHook()
{
BOOL rc;
rc = UnhookWindowsHookEx(g_hHoo
if (rc)
g_hHook = NULL;
return rc;
}
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(
g_hinstDll = hInstance; // Save DLL instance handle
break;
}
return TRUE;
}
--------------------------
Any mistake I have made?
Until now, I can only catch the msg when I press the keyboard button and my application is being active at the same time.
Thanks for your help!!!
It seems that your code is correct.
Try to replace TRACE with MessageBeep(-1) to make sure that TRACE works fine.
Try to replace TRACE with MessageBeep(-1) to make sure that TRACE works fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What you should know (perhaps you do already) is that your dll is loaded into every process - with a new copy! That means that e.g. the g_hHook variable is not initialized in the dll copies that are loaded in the other processes. Probably you should put this variable into a shared data block.
Regards, Madshi.