C # Hook in-thread message interception

Source: Internet
Author: User
Tags bool function prototype thread
The hook is actually a call to the API:

1, install Hook:
SetWindowsHookEx
Function prototype: Hhook SetWindowsHookEx (
int Idhook,//hook type,
HookProc LPFN,//hook function address
INSTANCE Hmod,//The handle of the instance where the hook is located,
The thread number of the thread that the DWORD dwthreadid//Hook is monitoring
)
Hmod: For the thread procedure hook, the parameter passes null;
For system hooks: a handle to a hook DLL with a parameter
dwThreadID: For global hooks, this parameter is null.
Hook type with wh_callwndproc=4 (message sent to window). triggered by SendMessage)
Return: Success: Return SetWindowsHookEx to return the installed hook handle;
Failure: NULL;

2, callback, you want to intercept the message is carried out here:
LRESULT WINAPI Myhookproc (
int ncode,//Specify whether the message needs to be processed
WPARAM WPARAM,//contains additional messages for this message
LPARAM LPARAM//contains additional messages for this message
)

3. Call the next hook
Lresult CallNextHookEx (
Hhook HHK,//is the handle to your own hook function. Use this handle to traverse the hook chain
int ncode,//pass the incoming parameters simply to CallNextHookEx
WPARAM WPARAM,//pass the incoming parameters simply to CallNextHookEx
LPARAM LPARAM//Pass the incoming parameters simply to CallNextHookEx
);

4, after use, remember to uninstall the hook Oh, otherwise your system will become very slow incomparable!
BOOL UnhookWindowsHookEx (
Hhook HHK//hook handle to uninstall.
)

Use the above APIs in C # to encapsulate it, you can use it directly!
Give an example of a thread hook (two form runs on the same thread):

Using System.Runtime.InteropServices;

public class Form1:System.Windows.Forms.Form
{
...
Define delegate (Hook function, for callback)
public delegate int HookProc (int code, INTPTR wparam, ref cwpstruct CWP);

function to install hooks
[DllImport ("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowsHookEx (int type, HookProc hook, IntPtr instance, int ThreadID);
function to call next hook
[DllImport ("User32.dll", CharSet = CharSet.Auto)]
public static extern int CallNextHookEx (IntPtr hookhandle, int code, INTPTR wparam, ref cwpstruct CWP);
Uninstall Hook
[DllImport ("User32.dll", CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx (IntPtr hookhandle);
Get form Thread ID
DllImport ("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId (INTPTR hwnd, int ID);

Private HookProc HookProc;
Private IntPtr hookhandle = IntPtr.Zero;

Public Form1 ()
{
....
Hook handling method
This.hookproc = new HookProc (MYHOOKPROC);
}

Start intercept.
private bool Starthook ()
{
Form2 f=new Form2 ();
F.show ()//plus this
Install hooks to intercept messages sent to Form2 by the system
This.hookhandle = SetWindowsHookEx (4, HookProc, IntPtr.Zero, GetWindowThreadProcessId (f.handle,0));
Return (this.hookhandle!= 0);
}


Stop Intercept
private bool Stophook ()
{
return UnhookWindowsHookEx (This.hookhandle);
}

Hook handler function, intercept the message here and do the processing
private int Myhookproc (int code, INTPTR wparam, ref cwpstruct CWP)
{
Switch (code)
{
Case 0:
Switch (cwp.message)
{
Case 0x0000f://wm_paint, intercept WM_PAINT message
Do something
Break
}
Break
}
Return CallNextHookEx (Hookhandle,code,wparam, ref CWP);
}

[StructLayout (LayoutKind.Sequential)]
public struct CWPSTRUCT
{
public INTPTR lparam;
Public INTPTR wparam;
public int message;
Public INTPTR hwnd;
}
}

public class Form2:System.Windows.Forms.Form
{
....
}





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.