Re: SetWindowsHookEx and WH_MOUSE
Trecius <Trecius@discussions.microsoft.com> wrote:
I've a question regarding SetWindowsHookEx with WH_MOUSE.
I have created a DLL that sets a hook, which captures the
WM_LBUTTONDOWN only ONCE. When it determines that the user clicked
the button, the hook should be released. Here's my code...
POINT WINAPI HookWindow(HWND hWndToHook)
g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
g_hHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc,
g_hInstance, GetWindowThreadProcessId(hWndToHook, NULL));
MessageBox(NULL, "Point acquired!", "Acquired!", MB_OK);
You appear to be installing a hook into a different process. This causes
your DLL to be loaded into that process. Now, a DLL has a separate copy
of global variables in every process it is loaded into. If you debug
your hook proc, you will find that no global variables are actually set.
See KB article KB100634 "How to specify both shared data and non-shared
data in a DLL in Visual C++". But realize that:
a) g_hInstance should not be shared this way - in fact, it should not be
shared at all, it is only meaningful within a process.
b) g_hEvent cannot be shared this way - event handles are only valid
within a single process. You need to use a named event, or some other
c) You have a race condition. Multiple mouse events may arrive into your
hook before the other process reacts to the signalled event and
uninstalls the hook.
With best wishes,
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925