Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C++

Hooks and DLLs

Rate me:
Please Sign up or sign in to vote.
4.89/5 (81 votes)
31 Mar 200111 min read 1.6M   13.3K   335  
There is a lot of confusion about how to set up and use global hook functions. This essay attempts to clear up some of these issues.
// MouseHook.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#define _COMPILING_44E531B1_14D3_11d5_A025_006067718D04
#include "MouseHook.h"

#pragma data_seg(".JOE")
HWND hWndServer = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.JOE,rws")

HINSTANCE hInst;
UINT UWM_MOUSEMOVE;
HHOOK hook;
static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                       DWORD  Reason, 
                       LPVOID Reserved
					 )
{
 switch(Reason)
    { /* reason */
     case DLL_PROCESS_ATTACH:
	hInst = hInstance;
	UWM_MOUSEMOVE = RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
	return TRUE;
     case DLL_PROCESS_DETACH:
	if(hWndServer != NULL)
	   clearMyHook(hWndServer);
	return TRUE;
    } /* reason */
    return TRUE;
}


/****************************************************************************
*                                 setMyHook
* Inputs:
*       HWND hWnd: Window to notify
* Result: BOOL
*       TRUE if successful
*	FALSE if error
* Effect: 
*       Sets the hook
****************************************************************************/

__declspec(dllexport) BOOL setMyHook(HWND hWnd)
    {
     if(hWndServer != NULL)
	return FALSE; // already hooked!
     hook = SetWindowsHookEx(WH_GETMESSAGE,
			    (HOOKPROC)msghook,
			    hInst,
			    0);
     if(hook != NULL)
	{ /* success */
	 hWndServer = hWnd;
	 return TRUE;
	} /* success */
     return FALSE; // failed to set hook
    } // setMyHook

/****************************************************************************
*                                 clearMyHook
* Inputs:
*       HWND hWnd: Window hook
* Result: BOOL
*       TRUE if successful
*	FALSE if error
* Effect: 
*       Removes the hook that has been set
****************************************************************************/

__declspec(dllexport) BOOL clearMyHook(HWND hWnd)
    {
     if(hWnd != hWndServer || hWnd == NULL)
	return FALSE;
     BOOL unhooked = UnhookWindowsHookEx(hook);
     if(unhooked)
	hWndServer = NULL;
     return unhooked;
    } // clearMyHook

/****************************************************************************
*                                   msghook
* Inputs:
*       int nCode: Code value
*	WPARAM wParam:
*	LPARAM lParam:
* Result: LRESULT
*       Either 0 or the result of CallNextHookEx
* Effect: 
*       Hook processing function. If the message is a mouse-move message,
*	posts the coordinates to the parent window
****************************************************************************/

static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam)
    {
     if(nCode < 0)
	{ /* pass it on */
	 CallNextHookEx(hook, nCode, wParam, lParam);
	 return 0;
	} /* pass it on */
     LPMSG msg = (LPMSG)lParam;
     if(msg->message == WM_MOUSEMOVE ||
	msg->message == WM_NCMOUSEMOVE)
	 PostMessage(hWndServer, UWM_MOUSEMOVE, 0, 0);
     return CallNextHookEx(hook, nCode, wParam, lParam);
    } // msghook

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions