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
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.