Code Monkey home page Code Monkey logo

DetourSharp

DetourSharp is a fully managed .NET library for interception of binary functions.

Sample

using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using DetourSharp;

unsafe
{
    // Before we install any hooks into it, we need to ensure
    // the target method has been jitted so it can be modified.
    RuntimeHelpers.PrepareMethod(typeof(Methods)
        .GetMethod(nameof(Methods.Target))!
        .MethodHandle
    );

    var pTarget = (delegate* unmanaged<void>)&Methods.Target;
    var pDetour = (delegate* unmanaged<void>)&Methods.Detour;

    using (var detour = new Detour(pTarget, pDetour))
    {
        // The Detour class provides a trampoline that can be used to bypass the hook.
        var pBypass = (delegate* unmanaged<void>)detour.TrampolineAddress;

        // Simply creating a Detour will not attach the hook to the target address.
        // In order to do that, you need to call the Attach() method it provides.
        pTarget(); // "Target"
        pDetour(); // "Detour"
        pBypass(); // "Target"

        // Once Attach() has been called, our method is hooked.
        detour.Attach();
        pTarget(); // "Detour"
        pDetour(); // "Detour"
        pBypass(); // "Target"
    }

    // When the Detour is disposed, it will be automatically detached.
    pTarget(); // "Target"
    pDetour(); // "Detour"
}

static class Methods
{
    [UnmanagedCallersOnly]
    public static void Target() => Console.WriteLine(nameof(Target));

    [UnmanagedCallersOnly]
    public static void Detour() => Console.WriteLine(nameof(Detour));
}

detoursharp's Projects

detoursharp icon detoursharp

A fully managed .NET library for interception of binary functions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.