Click here to Skip to main content
15,358,239 members
Articles / Programming Languages / C#
Posted 22 Sep 2003


192 bookmarked

Using Hooks from C#

Rate me:
Please Sign up or sign in to vote.
4.58/5 (39 votes)
30 Dec 2009CPOL5 min read
An article on using Windows hooks from .NET, demonstrated with a MouseHook.


MSDN's definition of a Windows hook is:

A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure.

While the .NET framework library has wrapped a significant portion of the Win32 API, there are numerous areas where we have to resort to P/Invoke to get at the functionality we need. Hooks are one of those areas. One likely reason for this is that hooks are pretty low-level things which most programs rarely need. Another is that they are intimately tied to the message based nature of the Windows API itself. They would likely not be portable to any other Operating System.

Windows hooks are implemented using callback functions. The callback procedure has a signature similar to the SendMessage API, so Windows hooks are very generic and are used to extend a number of different types of Windows message processing methods (see below). This library provides a C# wrapper around the hook procedure that can be used from a Windows Forms application.

A MouseHook is a particular type of Windows hook that allows your code to be notified when any window on a particular thread gets a mouse message. This example uses a mouse hook to capture the button up messages for the two navigation buttons on 5-button mice. By using a mouse hook, the application's main Form can control navigation via the mouse, regardless of what child control currently has input focus. If you don't have a 5-button mouse, the demo isn't going to be very exciting for you, but the code demonstrates the basic concepts.


Last summer, there was an interesting article, by Dino Esposito, in the MSDN magazine, about implementing hooks in C#. "Hmmm", I thought to myself, "That's interesting, but why in the world would you ever want to do that?"

Well, the application I'm currently working on uses a web browser-like "forward/back" idiom, wherein the user can navigate between views, much like they would a series of web pages. Each view is a UserControl that gets loaded dynamically as the user moves around the application.

I, for one, can no longer live without the forward and back buttons on my five button mouse, so I thought I'd add mouse navigation to our app.

I quickly realized that this was not as simple as it first appeared. Because each view UserControl takes up the entire client area of the Form it is hosted on, the Form itself never gets any MouseUp events. I didn't want to deal with this in each and every view I'd create, so I had to come up with a way to handle the mouse button navigation in one place.

My first idea was to attach to each view's MouseUp event in the Form and do the navigation from there. This quickly proved itself to be a kludge as each UserControl has its own child controls, and when they have focus, the UserControl doesn't get any mouse events either. To work, one would need to recursively attach all child control events to the same handler. Sounds messy, so scratch idea #1.

The next thought was to use the WM_PARENTNOTIFY message in the Form's WndProc. This looked promising at first and I had a proof of concept all whipped in no time. Then, I thought to myself, "The only mouse messages that spawns WM_PARENTNOTIFY are mouse down messages. I wonder when exactly a web browser navigates".

A quick test proved that web browsers navigate on mouse up, not mouse down (as, upon further reflection, one would expect). Scratch idea #2.

That led me to the implementation I was hoping I wouldn't have to delve into: a MouseHook. I recalled implementing these in VB6 (in what seems like the dark ages now) and the havoc that they could wreck upon the VB6 IDE. Well, it seemed like the only solution, so a quick Google search led me back to Dino's article, and after a quick look at the code, it began to appear that it wouldn't be as bad as I had feared.

Windows hooks are thread specific, and this implementation uses the thread of the code that calls the Install method, which if you call it synchronously from a Form, will be the main UI thread. This is perfect for my navigation problem because, now I can have code in the Form that will respond to a forward/back mouse click, no matter what control has focus.

The code download includes and uses the LocalWindowHook class which Dino Esposito used to demonstrate the basics of Windows hooks in the MSDN sample code.

Using the code

There are two classes you can use to get at the MouseHook.

The first is a class, MouseHook, that inherits directly from LocalWindowHook. Instantiate it, attach to its events, call Install(), and away you go. It will remove the hook when you Dispose it or when its finalizer runs.

private MouseHook hook = new MouseHook();

private void HookUp()
    this.hook.MouseUp += new MouseHookEventHandler( this.hook_MouseUp );

private void hook_MouseUp(object sender, MouseHookEventArgs e)
    // do some stuff with your exciting new mouse hook data

The second class is a System.ComponentModel.Component derived type that can be placed onto a design surface. In addition to making the MouseHook easier to use from the VS.NET IDE, the MouseHookComponent automatically installs the hook from the constructor that the Windows Forms Designer uses.

That means, all you really need to do is drop it on a Form and attach to the events you are interested in.

Future enhancements

The are a number of different types of hooks supported by the Windows API. Dino Esposito's LocalWindowsHook class is completely generic, and can be used to create any one of those hook types:

public enum HookType : int
    WH_KEYBOARD = 2,
    WH_CBT = 5,
    WH_MOUSE = 7,
    WH_HARDWARE = 8,
    WH_DEBUG = 9,
    WH_SHELL = 10,
    WH_KEYBOARD_LL = 13,
    WH_MOUSE_LL = 14

The MouseHook class I've created here is just one example of a derived class that abstracts the complexity of a particular kind of Windows hook. I've also included the CbtLocalHook class from the MSDN sample code.



  • 09/19/2003 - Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Don Kackman
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

GeneralMy vote of 4 Pin
Sivaji156526-Sep-13 23:51
MemberSivaji156526-Sep-13 23:51 
Questiongood article Pin
BillW337-Nov-12 4:39
professionalBillW337-Nov-12 4:39 
GeneralMy vote of 5 Pin
luoly1218-Aug-12 16:40
Memberluoly1218-Aug-12 16:40 
QuestionCan't run in VS2010 Pin
hambor29-Aug-11 3:16
Memberhambor29-Aug-11 3:16 
AnswerRe: Can't run in VS2010 Pin
wooohaa26-Jun-12 3:03
Memberwooohaa26-Jun-12 3:03 
QuestionGlobal CBTHook? Pin
Ziggy98118-Aug-10 21:13
MemberZiggy98118-Aug-10 21:13 
GeneralCool - If you like Hooks Pin
MarkJoel6021-Jan-10 4:57
MemberMarkJoel6021-Jan-10 4:57 
Generalin vb. Pin
rdpeake11-Jan-10 6:07
Memberrdpeake11-Jan-10 6:07 
GeneralRe: in vb. Pin
Don Kackman18-Jan-10 16:28
MemberDon Kackman18-Jan-10 16:28 
GeneralRe: in vb. Pin
rdpeake19-Jan-10 2:10
Memberrdpeake19-Jan-10 2:10 
GeneralRe: in vb. [modified] Pin
Don Kackman20-Jan-10 3:54
MemberDon Kackman20-Jan-10 3:54 
GeneralRe: in vb. Pin
rdpeake20-Jan-10 13:52
Memberrdpeake20-Jan-10 13:52 
GeneralWPF Pin
darrellp1-Jan-10 5:36
Memberdarrellp1-Jan-10 5:36 
GeneralRe: WPF Pin
Don Kackman2-Jan-10 8:36
MemberDon Kackman2-Jan-10 8:36 
GeneralRe: WPF Pin
darrellp2-Jan-10 10:16
Memberdarrellp2-Jan-10 10:16 
GeneralRe: WPF Pin
Don Kackman2-Jan-10 14:27
MemberDon Kackman2-Jan-10 14:27 
GeneralI didn't know it! Pin
yyhotspring30-Dec-09 17:41
Memberyyhotspring30-Dec-09 17:41 
GeneralAbove link is SPAM Pin
spoodygoon31-Dec-09 7:13
Memberspoodygoon31-Dec-09 7:13 
QuestionIs this a joke? Pin
Member 14391572-Dec-09 2:52
MemberMember 14391572-Dec-09 2:52 
AnswerRe: Is this a joke? Pin
Don Kackman2-Dec-09 4:05
MemberDon Kackman2-Dec-09 4:05 
GeneralHook ReadFile Pin
wei_future5-Jun-08 21:04
Memberwei_future5-Jun-08 21:04 
Questionc# DoModal in handler doesn't return?!? Pin
jfstephe1-Nov-07 8:20
Memberjfstephe1-Nov-07 8:20 
GeneralVB.Net Pin
westville_mike17-Oct-07 4:27
Memberwestville_mike17-Oct-07 4:27 
QuestionCancel events? Pin
Johnny J.23-Aug-07 22:40
professionalJohnny J.23-Aug-07 22:40 
QuestionHook on Insert Smart Card into Smart Card Reader Pin
KDSSCL17-Dec-06 20:21
MemberKDSSCL17-Dec-06 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.