C # hook this lens internal message interception

xiaoxiao2021-03-30  222

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 HMOD, // hook the unique handle, DWORD DWTHREADID // Hype of threads monitored by hooks: For the line program hook, parameter biol; for the system hook: Parameter is a hook DLL handle DWTHREADID: For global hook, this parameter is NULL. The hook type uses wh_callwndproc = 4 (send to the window message. Triggered by sendMessage): Success: Return to SetWindowsHookex Returns the installed hook handle; fail: null; 2, callback, you have to intercept the message here: LResult WinApi myhookProc (INT ncode, / / ​​Specify whether it needs to handle the message wparam wparam, // contains additional messages containing the message lParam lparam // contains additional messages for the message) 3, call the next hook LRESULT CallNexthookex (hHOOK hHK, // is you Handle of your own hook function. Use this handle to traverse the hook chain int ncode, // simply pass the incoming parameters to CallNexthookex can wparam wparam, // Simply pass the incoming parameters to CallNexthookex LParam lparam /// Simply pass the incoming parameters to CallNexthookex); 4, remember to uninstall the hook after you have finished using it! Bool UnhookWindowsHookex (hHOOK HHK / / Hook handle to uninstall.

Pack the above API with C # pack, you can use it directly! Give a threaded example (two forms run in the same thread): use system.Runtime.InterOpServices; public class form1: system.windows.forms.form {... // Define delegation (hook function, callback) public delegate int HookProc (int code, IntPtr wparam, ref CWPSTRUCT cwp); // mounting hook function [DllImport ( "User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowsHookEx (int type , HookProc hook, IntPtr instance, int threadID); // call to a 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 IntPtr hookHandle = IntPtr.Zero; public Form1 () {.... // articulated hook approach this. HookProc = New HookProc (My HookProc);} // Start blocking private bool starthook () {form2 f = new form2 (); f.show (); // plus this // Install hook, intercepting the system to Form2 THISHOOKHANDLE = SETWINDOWSHOKEX (4, hookProc, IntPtr.Zero, GetWindowThreadProcessId (f.Handle, 0)); return (this.hookHandle = 0!);} // intercepting private bool StopHook () {return UnhookWindowsHookEx (this.hookHandle);} / / Hook processing function,

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

New Post(0)