C # hook this lens internal message interception

xiaoxiao2021-03-06  99

The hook is actually calling an API:

1. Install the hook: setWindowshookex function protest: hHOOK SETWINDOWSHOKEX (int IDHOK, // hook type, hookproc lpfn, // hook function address instance of instance hmod, // hook, dword dwthreadid // Monkey monitored thread Thread number) HMOD: For the line program hook, parameter biol; for the system hook: Parameter is the handle of the hook DLL DWTHREADID: For global hook, this parameter is null. The hook type uses wh_callwndproc = 4 (send to the window. Triggered by sendMessage): Success: Returns SETWINDOWSHOKEX Returns the installed hook handle; failed: NULL;

2, callback, you want to intercept the message is here: LResult WinApi myhookProc (int ncode, // Specify whether you need to process the message wparam wparam, // Contains additional message containing the message LParam lparam // contains additional messages containing the message)

3, call the next hook LRESULT CALLNEXTHOKEX (HHOOK HHK, // is the handle of your own hook function. Use this handle to traverse the hook chain int ncode, // simply pass the incoming parameters to CallNexthookex WPARM WPARAM, / / Simply pass the incoming parameters to CallNexthookex, you can simply pass the incoming parameters to CallNexthookex;

4, remember to uninstall the hook after you have used it, or your system will become unparalleled! Bool UnHookWindowsHookex (hHOOK HHK / / Hook handle to uninstall.)

Use the above API with C # pack, you can use it directly! Give an example of a thread hook (two forms run in the same thread):

Using system.Runtime.InteropServices;

Public class form1: system.windows.forms.form {... // Defines a delegate (hook function, used to callback) Public Delegate Int hookProc (int Code, INTPTR WPARAM, REF CWPSTRUCT CWP);

// Install the function of the hook [DLLIMPORT ("User32.dll"] Public Static Extern INTPTR SETWINDOWSHOOKEX (INT TYPE, HOOKPROC HOOK, INTPTR Instance, INT ThreadID); // Call the next hook function [ DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern int CallNextHookEx (IntPtr hookHandle, int code, IntPtr wparam, ref CWPSTRUCT cwp); // unloading 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 inteptr hookhandle = intptr.zero;

PUBLIC FORM1 () {.... // Mounting hook processing method this.hookproc = new hookProc (myhookproc);

// Start blocking private bool starthook () {form2 f = new form2 (f.show (); // plus this // install hook, intercepting the system to Form2 Message this.HOOKHANDE = SETWINDOWSHOKEX (4, hookproc , INTPTR.ZERO, GETWINDOWTHREADPROCESSID (F.Handle, 0)); return (this.hookHandle! = 0);}

// Stop blocking private bool stophook () {return unhookwindowshookex (this.hookhandle);

// Hook processing function, intercept message here and do the proDyhookProc (int Code, INTPTR WPARAM, REF CWPSTRUCT CWP) {switch (code) {case 0: switch (cwp.Message) {case 0x0000f: // wm_paint, WM_PAINT message interception // 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 {....}

转载请注明原文地址:https://www.9cbs.com/read-105112.html

New Post(0)