• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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)"HookDll.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_KEYBOARD, KeyboardProc, g_hinstDll, 0);

   return (g_hHook != NULL);
}

__declspec(dllexport) BOOL UnSetHook()
{
     BOOL rc;
     rc = UnhookWindowsHookEx(g_hHook);
     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!!!
0
hon67
Asked:
hon67
1 Solution
 
MadshiCommented:
Hi, I'm a Delphi programmer, so I don't understand all you've written very fast. What does this TRACE exactly do?

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.
0
 
NickRepinCommented:
It seems that your code is correct.
Try to replace TRACE with MessageBeep(-1) to make sure that TRACE works fine.
0
 
jhanceCommented:
The Feb2000 issue of MSJ has an article on just this subject.  Here's a link to the article online and it includes a demo app that shows the use of it's methods:


http://www.microsoft.com/msj/0200/c++/c++0200.asp
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now