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

An inline hook. More...

#include <inline_hook.hpp>

Classes

struct  Error
 Error type for InlineHook. More...
 

Public Types

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

Public Member Functions

 InlineHook (const InlineHook &)=delete
 
 InlineHook (InlineHook &&other) noexcept
 
InlineHookoperator= (const InlineHook &)=delete
 
InlineHookoperator= (InlineHook &&other) noexcept
 
void reset ()
 Reset the hook.
 
uint8_t * target () const
 Get a pointer to the target.
 
uintptr_t target_address () const
 Get the target address.
 
uint8_t * destination () const
 Get a pointer ot the destination.
 
uintptr_t destination_address () const
 Get the destination address.
 
const Allocationtrampoline () const
 Get the trampoline Allocation.
 
 operator bool () const
 Tests if the hook is valid.
 
template<typename T>
original () const
 Returns the address of the trampoline to call the original function.
 
const auto & original_bytes () const
 Returns a vector containing the original bytes of the target function.
 
template<typename RetT = void, typename... Args>
RetT call (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT ccall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT thiscall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT stdcall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT fastcall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT unsafe_call (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT unsafe_ccall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT unsafe_thiscall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT unsafe_stdcall (Args... args)
 Calls the original function.
 
template<typename RetT = void, typename... Args>
RetT unsafe_fastcall (Args... args)
 Calls the original function.
 
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< InlineHook, Errorcreate (void *target, void *destination, Flags flags=Default)
 Create an inline hook.
 
template<typename T, typename U>
static std::expected< InlineHook, Errorcreate (T target, U destination, Flags flags=Default)
 Create an inline hook.
 
static std::expected< InlineHook, Errorcreate (const std::shared_ptr< Allocator > &allocator, void *target, void *destination, Flags flags=Default)
 Create an inline hook with a given Allocator.
 
template<typename T, typename U>
static std::expected< InlineHook, Errorcreate (const std::shared_ptr< Allocator > &allocator, T target, U destination, Flags flags=Default)
 Create an inline hook with a given Allocator.
 

Friends

class MidHook
 

Detailed Description

An inline hook.

Member Enumeration Documentation

◆ Flags

Flags for InlineHook.

Enumerator
Default 

Default flags.

StartDisabled 

Start the hook disabled.

Member Function Documentation

◆ call()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::call ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the default calling convention set by your compiler.

◆ ccall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::ccall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __cdecl calling convention.

◆ create() [1/4]

template<typename T, typename U>
static std::expected< InlineHook, Error > safetyhook::InlineHook::create ( const std::shared_ptr< Allocator > & allocator,
T target,
U destination,
Flags flags = Default )
inlinestaticnodiscard

Create an inline hook with a given Allocator.

Parameters
allocatorThe allocator to use.
targetThe address of the function to hook.
destinationThe destination address.
flagsThe flags to use.
Returns
The InlineHook or an InlineHook::Error if an error occurred.
Note
If you don't care about error handling, use the easy API (safetyhook::create_inline).

◆ create() [2/4]

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

Create an inline hook with a given Allocator.

Parameters
allocatorThe allocator to use.
targetThe address of the function to hook.
destinationThe destination address.
flagsThe flags to use.
Returns
The InlineHook or an InlineHook::Error if an error occurred.
Note
If you don't care about error handling, use the easy API (safetyhook::create_inline).

◆ create() [3/4]

template<typename T, typename U>
static std::expected< InlineHook, Error > safetyhook::InlineHook::create ( T target,
U destination,
Flags flags = Default )
inlinestaticnodiscard

Create an inline hook.

Parameters
targetThe address of the function to hook.
destinationThe destination address.
flagsThe flags to use.
Returns
The InlineHook or an InlineHook::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_inline).

◆ create() [4/4]

static std::expected< InlineHook, Error > safetyhook::InlineHook::create ( void * target,
void * destination,
Flags flags = Default )
staticnodiscard

Create an inline hook.

Parameters
targetThe address of the function to hook.
destinationThe destination address.
flagsThe flags to use.
Returns
The InlineHook or an InlineHook::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_inline).

◆ destination()

uint8_t * safetyhook::InlineHook::destination ( ) const
inlinenodiscard

Get a pointer ot the destination.

Returns
A pointer to the destination.

◆ destination_address()

uintptr_t safetyhook::InlineHook::destination_address ( ) const
inlinenodiscard

Get the destination address.

Returns
The destination address.

◆ fastcall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::fastcall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __fastcall calling convention.

◆ operator bool()

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

Tests if the hook is valid.

Returns
True if the hook is valid, false otherwise.

◆ original()

template<typename T>
T safetyhook::InlineHook::original ( ) const
inlinenodiscard

Returns the address of the trampoline to call the original function.

Template Parameters
TThe type of the function pointer.
Returns
The address of the trampoline to call the original function.

◆ original_bytes()

const auto & safetyhook::InlineHook::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::InlineHook::reset ( )

Reset the hook.

This will restore the original function and remove the hook.

Note
This is called automatically in the destructor.

◆ stdcall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::stdcall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __stdcall calling convention.

◆ target()

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

Get a pointer to the target.

Returns
A pointer to the target.

◆ target_address()

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

Get the target address.

Returns
The target address.

◆ thiscall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::thiscall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __thiscall calling convention.

◆ trampoline()

const Allocation & safetyhook::InlineHook::trampoline ( ) const
inlinenodiscard

Get the trampoline Allocation.

Returns
The trampoline Allocation.

◆ unsafe_call()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::unsafe_call ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the default calling convention set by your compiler.
This function is unsafe because it doesn't lock the mutex. Only use this if you don't care about unhook safety or are worried about the performance cost of locking the mutex.

◆ unsafe_ccall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::unsafe_ccall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __cdecl calling convention.
This function is unsafe because it doesn't lock the mutex. Only use this if you don't care about unhook safety or are worried about the performance cost of locking the mutex.

◆ unsafe_fastcall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::unsafe_fastcall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __fastcall calling convention.
This function is unsafe because it doesn't lock the mutex. Only use this if you don't care about unhook safety or are worried about the performance cost of locking the mutex.

◆ unsafe_stdcall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::unsafe_stdcall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __stdcall calling convention.
This function is unsafe because it doesn't lock the mutex. Only use this if you don't care about unhook safety or are worried about the performance cost of locking the mutex.

◆ unsafe_thiscall()

template<typename RetT = void, typename... Args>
RetT safetyhook::InlineHook::unsafe_thiscall ( Args... args)
inline

Calls the original function.

Template Parameters
RetTThe return type of the function.
...ArgsThe argument types of the function.
Parameters
...argsThe arguments to pass to the function.
Returns
The result of calling the original function.
Note
This function will use the __thiscall calling convention.
This function is unsafe because it doesn't lock the mutex. Only use this if you don't care about unhook safety or are worried about the performance cost of locking the mutex.

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