CoolHook 1.0.0
dotnet add package CoolHook --version 1.0.0
NuGet\Install-Package CoolHook -Version 1.0.0
<PackageReference Include="CoolHook" Version="1.0.0" />
<PackageVersion Include="CoolHook" Version="1.0.0" />
<PackageReference Include="CoolHook" />
paket add CoolHook --version 1.0.0
#r "nuget: CoolHook, 1.0.0"
#:package CoolHook@1.0.0
#addin nuget:?package=CoolHook&version=1.0.0
#tool nuget:?package=CoolHook&version=1.0.0
β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 | Versions 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. |
-
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 |