You are here:

C++/Low Level Mouse Hook and DLL


Hi David

I'm trying to create an app that will allow me to control 2 pc's with one mouse and keyboard. (A bit like VNC, but no return video feed - just injecting mouse and keyboard via TCP onto a neighbouring PC) At the moment, let's concentrate only on the mouse.

I've created a DLL that has a number of functions.

Exported functions:

To setup a pointer to a queue that will be used to put hook messages on for sending to remote desktops...
void SetHCIDetails(HGLQueue* &pQueue, const int iNumSlaves)
   g_pHCISenderQueue = pQueue;
   g_iNumSlaves      = iNumSlaves;

To setup a hook to grab the low level mouse messages...
   g_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)&g_hMouseHook, hDLL, 0);
   return g_hMouseHook;

Not exported:
LRESULT CALLBACK MouseHook(int nCode, WPARAM wMouseMessage, LPARAM lMouseCoords)

In my calling app - I load the hook like this
   m_hMyDll = LoadLibrary(strDLLPath.c_str());

   SetHCIDetails(m_pQueue, 1);
   m_hMouseHook = HookMouse(m_hMyDll);

However, as soon as I move my mouse I get an exception error - read of address 0x00000 - and I have no idea why!

Am I doing something stupid?

Steve, Thank you for your question.

If you are saving global information (such as a hook handle), be sure to save it in such a way that callers can access it from any process. In general, a hook procedure is executed in the context (address space) of the current user process (when a mouse is moved, any process may be active).

Very few types of "global" storage are truly global; they  include window Properties and the contents of memory-mapped files.

Use a debug window and output process IDs and hook handles to verify that your programs access global data correctly.

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 All rights reserved.