CoolHook 1.0.0

dotnet add package CoolHook --version 1.0.0
                    
NuGet\Install-Package CoolHook -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CoolHook" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CoolHook" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CoolHook" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CoolHook --version 1.0.0
                    
#r "nuget: CoolHook, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CoolHook@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CoolHook&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CoolHook&version=1.0.0
                    
Install as a Cake Tool

βœ”CoolHook

CoolHook is a library for managing method hooks in .NET applications. This library allows you to dynamically hook methods at runtime and replace them with custom implementations.

Features 🌟

  • Create Hooks: Dynamically hook methods at runtime. πŸ”„
  • Remove Hooks: Easily remove hooks and restore original methods. ❌
  • Manage Hooks: Retrieve and remove hooks by name or method. πŸ—‚οΈ

Installation πŸ“¦

To use CoolHook, add the library to your project via NuGet or by referencing the compiled assembly. πŸ”§

Usage πŸ› οΈ

Csproj Configure

Before adding the source code, you need to configure the csproj file. Just add this XML code there:

	<PropertyGroup Condition=" '$(Platform)' == 'x64' ">
		<DefineConstants>WIN64;$(DefineConstants)</DefineConstants>
	</PropertyGroup>
	
	<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
		<DefineConstants>WIN32;$(DefineConstants)</DefineConstants>
	</PropertyGroup>

HookManagerπŸ”

The HookManager class manages the creation, removal, and retrieval of method hooks.

Properties
  • EnabledHooks: Returns the number of currently enabled hooks.πŸ“ˆ
Methods
  • CreateHook(MethodBase methodBase, MethodBase hookedMethod, string name = null): Creates a new hook for the specified methods. Optionally, associate a name with the hook.✨
  • RemoveAllHooks(): Removes all hooks and restores original methods.🚫
  • RemoveHook(Hook hook): Removes a specific hook.πŸ”
  • GetHookByName(string name): Retrieves a hook by its name.πŸ“œ
  • RemoveHookByName(string name): Removes a hook by its name.❌
  • HasHook(MethodBase methodBase): Checks if a hook exists for the specified method.βœ”οΈ
  • GetHooksForMethodType(Type methodType): Retrieves all hooks associated with a specific method type.πŸ“‚

Hook

The Hook class represents a single method hook.

Properties
  • BaseMethod: The method being hooked.πŸ”—
  • BaseMethodPointer: Pointer to the base method.🧭
  • HookedMethodPointer: Pointer to the hooked method.🧭
Constructors
  • Hook(MethodBase baseMethod, MethodBase hookedMethod): Creates a hook for the specified methods.πŸ”¨
  • Hook((MethodBase, MethodBase) methodsToHook): Creates a hook from a tuple of methods.🧩
  • Hook(Type baseType, string baseMethodName, Type hookedType, string hookedMethodName, BindingFlags bindingFlags): Creates a hook using method names and types.πŸ”§
  • Hook(IntPtr baseMethodPtr, IntPtr hookedMethodPtr): Creates a hook using method pointers.πŸ“Œ
Methods
  • SetHook(): Applies the hook to the base method.βœ…
  • RemoveHook(): Removes the hook and restores the original method.🚫

ExampleπŸ’‘

Here is an example demonstrating how to use CoolHook:

using CoolHook;
using System.Reflection;

class Program
{
    static void Main()
    {
        // Create a HookManager instance
        HookManager hookManager = new HookManager();

        // Get methods to hook
        var methods = GetMethodsToHook();

        // Create a hook
        hookManager.CreateHook(methods.baseMethod, methods.hookMethod);

        // Verify hooks
        Console.WriteLine("Method Hooked!");
        Console.WriteLine(hookManager.GetHooksForMethodType(typeof(Program)).Count);

        // Call hooked methods
        BaseMethod(); // Outputs: "it's hook method!"
        HookMethod(); // Outputs: "it's hook method!"

        // Remove all hooks
        hookManager.RemoveAllHooks();
        Console.WriteLine("Removed all method hooks!");

        // Call original methods
        BaseMethod(); // Outputs: "It's original method!"
        HookMethod(); // Outputs: "It's hook method!"
    }

    static (MethodBase baseMethod, MethodBase hookMethod) GetMethodsToHook()
    {
        return (typeof(Program).GetMethod("BaseMethod", BindingFlags.Static | BindingFlags.NonPublic),
                typeof(Program).GetMethod("HookMethod", BindingFlags.Static | BindingFlags.NonPublic));
    }

    static void BaseMethod()
    {
        Console.WriteLine("It's original method!");
    }

    static void HookMethod()
    {
        Console.WriteLine("It's hook method!");
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 206 9/16/2024