C # Hook Learning notes _c# Tutorial

Source: Internet
Author: User
Tags message queue
Includes system hooks and thread hooks, or global hooks and private hooks. The system hook needs a separate DLL, this place delays a lot of time, the internet has said can not use a separate DLL.
Now it's time to put the code in the code, referring to an article in the Red-Black alliance.
Copy Code code as follows:

public class Sethook
{
public class Hooktypes
{
<summary>
Hook type
</summary>
public enum Hooktype
{
Wh_journalrecord = 0,//Record the input messages sent to the message queue
Wh_getmessage = 3,//to monitor messages sent to message queues
Wh_journalplayback = 1,//To send messages that were previously recorded by the Wh_journalrecord hook process
Wh_callwndproc = 4,//to monitor the message before it sends the message to the target window process
WH_CBT = 5,//Accept messages that are useful to the CBT application
Wh_msgfilter = -1,//Monitor messages raised by dialog boxes, message boxes, menu bars, or input events in scrollbars
Wh_sysmsgfilter = 6,//Monitor messages raised by dialog boxes, message boxes, menu bars, scroll bars, input events
8
Wh_debug = 9,//The other hook processing process to debug
Wh_shell = 10,//accepts notifications that are useful to shell applications
Wh_foregroundidle = 11,//is invoked when the application's foreground thread is about to enter an idle state, which helps to perform low-priority tasks during idle time
#region Mouse and keyboard events
<summary>
To monitor keystroke messages
</summary>
Wh_keyboard = 2,
<summary>
Can only be installed in Windows NT to monitor underlying keyboard input events
</summary>
Wh_keyboard_ll = 13,
<summary>
Monitor Mouse messages
</summary>
Wh_mouse = 7,
<summary>
Can only be installed in Windows NT to monitor underlying mouse events
</summary>
Wh_mouse_ll = 14,
#endregion
}
}
Public abstract class Hooks:hooktypes
{
public delegate int HookProc (int ncode, int wParam, IntPtr iparam);
<summary>
Installing hooks
</summary>
<param name= "Idhook" > Hook type, which is the message type it handles </param>
<param name= "LPFN" > Hook function Address </param>
<param name= "HInstance" > the handle to the application instance. Identifies the dll</param> that contains the Cheng that LPFN refers to
<param name= "ThreadId" The ID number of the thread that you want to monitor after installing the hook, if 0, the hook thread is associated with all threads, that is, the global hook </param>
<returns> return parameter is hook handle, failure is null</returns>
[DllImport ("user32.dll", CallingConvention = Callingconvention.stdcall)]
public static extern int SetWindowsHookEx (Hooktype hooktype, HookProc lpfn, IntPtr hinstance, int threadId);
<summary>
Uninstall Hook
</summary>
<param name= "Idhook" > Hook handle to uninstall </param>
<returns></returns>
[DllImport ("user32.dll", CallingConvention = Callingconvention.stdcall)]
public static extern bool UnhookWindowsHookEx (int idhook);
<summary>
Continue to the next hook
</summary>
<param name= "Idhook" ></param>
<param name= "Ncode" ></param>
<param name= "WParam" ></param>
<param name= "Iparam" ></param>
<returns></returns>
[DllImport ("user32.dll", CallingConvention = Callingconvention.stdcall)]
public static extern int CallNextHookEx (int idhook, int ncode, int wParam, IntPtr iparam);
<summary>
Get current thread number
</summary>
<returns></returns>
[DllImport ("kernel32")]
public static extern int GetCurrentThreadID ();
#region
<summary>
Delegate Object
</summary>
Public HookProc proc;
public abstract int SetWindowsHookEx ();
#endregion
}
public class Addhook:hooks
{
Hooktype Hooktyp;
HookProc HookProc;
Public Addhook (Hooktype _hooktype, HookProc _hookproc)
{
This.hooktyp = _hooktype;
This.hookproc = _hookproc;
}
public int Addprivatehook ()
{
return SetWindowsHookEx ();
}
<summary>
Thread Hooks
</summary>
<returns></returns>
public override int SetWindowsHookEx ()
{
int theadid = System.Threading.Thread.CurrentThread.ManagedThreadId (); Coping with the obsolescence of GetCurrentThreadID ()
int hookid = 0;
Object hookid_ = SetWindowsHookEx (This.hooktyp, This.hookproc, IntPtr.Zero, GetCurrentThreadID ());
if (hookid_!= null)
{
Hookid = (int) hookid_;
}
return hookid;
}
The system hook is similar to this, and the two parameters are different when the hook is installed.
}
}

Call me create a new class that makes it easy to call threads or system hooks, no more here.

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.