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



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();


4.) At the OnDestroy Function



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)
     else if (code == HC_NOREMOVE)

      // 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)
                     g_hinstDll = hInstance;  // Save DLL instance handle


     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!!!
1 Solution
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.
It seems that your code is correct.
Try to replace TRACE with MessageBeep(-1) to make sure that TRACE works fine.
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:


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