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!!!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.