SafetyHook: InlineHook Class Reference

SafetyHook

Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
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
InlineHook & | operator= (const InlineHook &)=delete
InlineHook & | operator= (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 Allocation & | trampoline () const
| Get the trampoline Allocation.
| operator bool () const
| Tests if the hook is valid.
template<typename T>
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, Error > | enable ()
| Enable the hook.
std::expected< void, Error > | disable ()
| Disable the hook.
bool | enabled () const
| Check if the hook is enabled.

## Static Public Member Functions
static std::expected< InlineHook, Error > | create (void *target, void *destination, Flags flags=Default)
| Create an inline hook.
template<typename T, typename U>
static std::expected< InlineHook, Error > | create (T target, U destination, Flags flags=Default)
| Create an inline hook.
static std::expected< InlineHook, Error > | create (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, Error > | create (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

enum safetyhook::InlineHook::Flags : int

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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  allocator | The allocator to use.
  target | The address of the function to hook.
  destination | The destination address.
  flags | The 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

  allocator | The allocator to use.
  target | The address of the function to hook.
  destination | The destination address.
  flags | The 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

  target | The address of the function to hook.
  destination | The destination address.
  flags | The 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

  target | The address of the function to hook.
  destination | The destination address.
  flags | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  T | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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

  RetT | The return type of the function.
  ...Args | The argument types of the function.

Parameters

  ...args | The 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:

- C:/M/B/src/safetyhook-0.6.5/include/safetyhook/inline_hook.hpp