SafetyHook
Loading...
Searching...
No Matches
safetyhook::MidHook Class Referencefinal

A mid function hook. More...

#include <mid_hook.hpp>

Classes

struct  Error
 Error type for MidHook. More...

Public Types

enum  Flags : int { Default = 0 , StartDisabled = 1 }
 Flags for MidHook. More...

Public Member Functions

 MidHook (const MidHook &)=delete
 MidHook (MidHook &&other) noexcept
MidHookoperator= (const MidHook &)=delete
MidHookoperator= (MidHook &&other) noexcept
void reset ()
 Reset the hook.
uint8_t * target () const
 Get a pointer to the target.
uintptr_t target_address () const
 Get the address of the target.
MidHookFn destination () const
 Get the destination function.
const auto & original_bytes () const
 Returns a vector containing the original bytes of the target function.
 operator bool () const
 Tests if the hook is valid.
std::expected< void, Errorenable ()
 Enable the hook.
std::expected< void, Errordisable ()
 Disable the hook.
bool enabled () const
 Check if the hook is enabled.

Static Public Member Functions

static std::expected< MidHook, Errorcreate (void *target, MidHookFn destination_fn, Flags flags=Default)
 Creates a new MidHook object.
template<typename T>
static std::expected< MidHook, Errorcreate (T target, MidHookFn destination_fn, Flags flags=Default)
 Creates a new MidHook object.
static std::expected< MidHook, Errorcreate (const std::shared_ptr< Allocator > &allocator, void *target, MidHookFn destination_fn, Flags flags=Default)
 Creates a new MidHook object with a given Allocator.
template<typename T>
static std::expected< MidHook, Errorcreate (const std::shared_ptr< Allocator > &allocator, T target, MidHookFn destination_fn, Flags flags=Default)
 Creates a new MidHook object with a given Allocator.

Detailed Description

A mid function hook.

Member Enumeration Documentation

◆ Flags

Flags for MidHook.

Enumerator
Default 

Default flags.

StartDisabled 

Start the hook disabled.

Member Function Documentation

◆ create() [1/4]

template<typename T>
std::expected< MidHook, Error > safetyhook::MidHook::create ( const std::shared_ptr< Allocator > & allocator,
T target,
MidHookFn destination_fn,
Flags flags = Default )
inlinestaticnodiscard

Creates a new MidHook object with a given Allocator.

Template Parameters
TThe type of the function to hook.
Parameters
allocatorThe Allocator to use.
targetThe address of the function to hook.
destination_fnThe destination function.
flagsThe flags to use.
Returns
The MidHook object or a MidHook::Error if an error occurred.
Note
If you don't care about error handling, use the easy API (safetyhook::create_mid).

◆ create() [2/4]

std::expected< MidHook, Error > safetyhook::MidHook::create ( const std::shared_ptr< Allocator > & allocator,
void * target,
MidHookFn destination_fn,
Flags flags = Default )
staticnodiscard

Creates a new MidHook object with a given Allocator.

Parameters
allocatorThe Allocator to use.
targetThe address of the function to hook.
destination_fnThe destination function.
flagsThe flags to use.
Returns
The MidHook object or a MidHook::Error if an error occurred.
Note
If you don't care about error handling, use the easy API (safetyhook::create_mid).

◆ create() [3/4]

template<typename T>
std::expected< MidHook, Error > safetyhook::MidHook::create ( T target,
MidHookFn destination_fn,
Flags flags = Default )
inlinestaticnodiscard

Creates a new MidHook object.

Parameters
targetThe address of the function to hook.
destination_fnThe destination function.
flagsThe flags to use.
Returns
The MidHook object or a MidHook::Error if an error occurred.
Note
This will use the default global Allocator.
If you don't care about error handling, use the easy API (safetyhook::create_mid).

◆ create() [4/4]

std::expected< MidHook, Error > safetyhook::MidHook::create ( void * target,
MidHookFn destination_fn,
Flags flags = Default )
staticnodiscard

Creates a new MidHook object.

Parameters
targetThe address of the function to hook.
destination_fnThe destination function.
flagsThe flags to use.
Returns
The MidHook object or a MidHook::Error if an error occurred.
Note
This will use the default global Allocator.
If you don't care about error handling, use the easy API (safetyhook::create_mid).

◆ destination()

MidHookFn safetyhook::MidHook::destination ( ) const
inlinenodiscard

Get the destination function.

Returns
The destination function.

◆ operator bool()

safetyhook::MidHook::operator bool ( ) const
inlineexplicit

Tests if the hook is valid.

Returns
true if the hook is valid, false otherwise.

◆ original_bytes()

const auto & safetyhook::MidHook::original_bytes ( ) const
inlinenodiscard

Returns a vector containing the original bytes of the target function.

Returns
A vector of the original bytes of the target function.

◆ reset()

void safetyhook::MidHook::reset ( )

Reset the hook.

This will remove the hook and free the stub.

Note
This is called automatically in the destructor.

◆ target()

uint8_t * safetyhook::MidHook::target ( ) const
inlinenodiscard

Get a pointer to the target.

Returns
A pointer to the target.

◆ target_address()

uintptr_t safetyhook::MidHook::target_address ( ) const
inlinenodiscard

Get the address of the target.

Returns
The address of the target.

The documentation for this class was generated from the following file: