Applying hooks in C #

Source: Internet
Author: User
Hooks, a platform for Windows Messaging mechanisms, where applications can set up a subroutine to monitor a message for a specified window, and the monitored window can be created by another process. When the message arrives, it is processed before the target window processes the function. The hook mechanism allows the application to intercept processing window messages or specific events.

A detailed description of the hook, in Microsoft's MSDN, http://www.microsoft.com/china/community/program/originalarticles/techdoc/hook.mspx

Here's how I apply hooks in C #:

Implementation effect:

When the user enters B in the textbox, the textbox always displays a

Implementation process:

1, a new C # windowsapplication

2, in Form1, add some of the following variables:

Internal enum Hooktype//enum, type of hook

{

Msgfilter =-1,

Journalrecord = 0,

Journalplayback = 1,

Keyboard = 2,

GetMessage = 3,

Callwndproc = 4,

CBT = 5,

Sysmsgfilter = 6,

Mouse = 7,

Hardware = 8,

Debug = 9,

Shell = 10,

Foregroundidle = 11,

Callwndprocret = 12,

Keyboardll = 13,

Mousell = 14,

};



IntPtr _nexthookptr; Record Hook number


3, the introduction of the necessary API in Form1

[DllImport ("kernel32.dll")]
static extern int GetCurrentThreadID (); Gets the API for the current thread number

[DllImport ("User32.dll")]

internal extern static void UnhookWindowsHookEx (INTPTR handle); Cancel the Hook API



[DllImport ("User32.dll")]

internal extern static IntPtr SetWindowsHookEx (int idhook, [MarshalAs (unmanagedtype.functionptr)] HookProc LPFN, INTPTR hinstance, int threadID); Set up the Hook API



[DllImport ("User32.dll")]

internal extern static IntPtr CallNextHookEx (IntPtr handle, int code, INTPTR wparam, INTPTR lparam); Get the next Hook API

4, declare a implementation of the delegate

Internal delegate IntPtr hookproc (int code, INTPTR wparam, INTPTR lparam);

5, add your own hook processing process


INTPTR myhookproc (int code, INTPTR wparam, INTPTR lparam)

{

if (Code < 0) return CallNextHookEx (_nexthookptr,code, wparam, lparam); Go back and let the following program process the message



if (wparam. ToInt32 () = = 98 | | wparam. ToInt32 () = = 66)//If the user enters a B

{

This.textBox1.Text = "a";



Return (INTPTR) 1; Returned directly, the message was disposed of at the end of the

}

Else

{

return IntPtr.Zero; Go back and let the following program process the message

}



}



6. Add a function to join the hook chain and cancel from the hook chain



public void Sethook ()

{

if (_nexthookptr!= IntPtr.Zero)//has been checked.



Return



HookProc Myhookproc = new HookProc (MYHOOKPROC); Declare a delegate object of one's own hook implementation function



_nexthookptr = SetWindowsHookEx (int) Hooktype.keyboard, Myhookproc, IntPtr.Zero, GetCurrentThreadID ()); Add to the hook chain

}



public void Unhook ()

{

if (_nexthookptr!= IntPtr.Zero)

{

UnhookWindowsHookEx (_NEXTHOOKPTR); Cancel from the hook chain



_nexthookptr = IntPtr.Zero;

}

}



7. Add Sethook () to the Form1 Load event, add Unhook in Form1 closing event ()



private void Form1_Load (object sender, System.EventArgs e)

{

Sethook ();

}



private void Form1_Closing (object sender, System.ComponentModel.CancelEventArgs e)

{

Unhook ();

}



8, Operation

Input B, found inside the textbox is a!


Summary:

This is a very simple hook application that may be used when we are working on a Windows program. I'm trying to do a more generic class to encapsulate it for better use later. Try ing ...



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.