SoFunction
Updated on 2025-03-07

C# Hook Study Notes


public class SetHook
{
public class HookTypes
{
/// <summary>
/// Hook type
/// </summary>
public enum HookType
{
WH_JOURNALRECORD = 0,//Record input messages sent to the message queue
WH_GETMESSAGE = 3,// Monitor messages sent to the message queue
WH_JOURNALPLAYBACK = 1,//Send messages previously recorded by WH_JOURNALRECORD hook processing process
WH_CALLWNDPROC = 4,// Monitor the message before the system sends it to the target window processing process.
WH_CBT = 5,//Accept messages that are useful for CBT applications
WH_MSGFILTER = -1,// Monitor messages triggered by input events in dialog boxes, message boxes, menu bars, or scroll bars
WH_SYSMSGFILTER = 6,// Monitor messages raised by input events in dialog boxes, message boxes, menu bars, scroll bars
//8
WH_DEBUG = 9,//Debug other hook processing processes
WH_SHELL = 10,//Accept notifications useful to shell applications
WH_FOREGROUNDIDLE = 11,// When the foreground thread of the application is about to enter the idle state, it helps to perform low-priority tasks during idle time
#region Mouse and keyboard events
/// <summary>
/// Monitor keystroke messages
/// </summary>
WH_KEYBOARD = 2,
/// <summary>
/// Only installed in Windows NT, used to monitor the underlying keyboard input events
/// </summary>
WH_KEYBOARD_LL = 13,
/// <summary>
/// Monitor mouse messages
/// </summary>
WH_MOUSE = 7,
/// <summary>
/// Only 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>
/// Install the hook
/// </summary>
/// <param name="idHook">hook type, that is, the type of message it processes</param>
/// <param name="lpfn"> hook function address</param>
/// <param name="hInstance">Handle of the application instance. Identify the DLL containing the subroutine referred to by lpfn</param>
/// <param name="threadId">The ID number of the thread you want to monitor after installing the hook. If it is 0, the hook child is associated with all threads, which is the global hook</param>
/// <returns>Return parameter is a hook handle, failed to be NULL</returns>
[DllImport("", CallingConvention = )]
public static extern int SetWindowsHookEx(HookType hooktype, HookProc lpfn, IntPtr hInstance, int threadId);
/// <summary>
/// Unload hook
/// </summary>
/// <param name="idHook">Hook handle to be uninstalled</param>
/// <returns></returns>
[DllImport("", CallingConvention = )]
public static extern bool UnhookWindowsHookEx(int idHook);
/// <summary>
/// Continue with the next hook
/// </summary>
/// <param name="idHook"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="IParam"></param>
/// <returns></returns>
[DllImport("", CallingConvention = )]
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr IParam);
/// <summary>
/// Get the 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)
{
= _hooktype;
= _hookproc;
}
public int AddPrivateHook()
{
return SetWindowsHookEx();
}
/// <summary>
/// Thread hook
/// </summary>
/// <returns></returns>
public override int SetWindowsHookEx()
{
//int theadId = (); deal with the obsoleteness of GetCurrentThreadId()
int hookId = 0;
object hookId_ = SetWindowsHookEx(, , , GetCurrentThreadId());
if (hookId_ != null)
{
hookId = (int)hookId_;
}
return hookId;
}
//The system hook is similar to this, the last two parameters are different when installing the hook
}
}