CommonLib
Loading...
Searching...
No Matches
Hooking.h File Reference
#include <functional>
#include <../thirdparty/Detours/src/detours.h>

Go to the source code of this file.

Namespaces

namespace  hedgedev
namespace  hedgedev::csl
namespace  hedgedev::csl::hook

Macros

#define FUNCTION_PTR(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS, ...)
#define IMPORT_FUNCTION_PTR(RETURN_TYPE, LIBRARY_NAME, FUNCTION_NAME, ...)
#define HOOK(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS, ...)
#define STATIC_HOOK(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS, ...)
#define GET_STATIC_HOOK_RESULT(FUNCTION_NAME)
#define VFTABLE_HOOK(RETURN_TYPE, CALLING_CONVENTION, CLASS_NAME, FUNCTION_NAME, ...)
#define STATIC_VFTABLE_HOOK(RETURN_TYPE, CALLING_CONVENTION, CLASS_NAME, FUNCTION_NAME, ...)
#define GET_STATIC_VFTABLE_HOOK_RESULT(CLASS_NAME, FUNCTION_NAME)
#define INSTALL_HOOK(FUNCTION_NAME)
#define INSTALL_HOOK_EXPLICIT(FUNCTION_NAME, ADDRESS)
#define UNINSTALL_HOOK(FUNCTION_NAME)
#define INSTALL_VFTABLE_HOOK(CLASS_NAME, INSTANCE, FUNCTION_NAME, FUNCTION_INDEX)
#define UNINSTALL_VFTABLE_HOOK(CLASS_NAME, FUNCTION_NAME)

Functions

void * hedgedev::csl::hook::GetPostHookAddress (void *in_pHookStart)

Macro Definition Documentation

◆ FUNCTION_PTR

#define FUNCTION_PTR ( RETURN_TYPE,
CALLING_CONVENTION,
FUNCTION_NAME,
ADDRESS,
... )
Value:
RETURN_TYPE (CALLING_CONVENTION* FUNCTION_NAME)(__VA_ARGS__) = (RETURN_TYPE (CALLING_CONVENTION*)(__VA_ARGS__))(ADDRESS)

Declares a pointer to a function in memory.

Parameters
RETURN_TYPEThe return type of the function.
CALLING_CONVENTIONThe calling convention of the function (e.g. __cdecl, __stdcall, __fastcall, etc). This does not support optimised calling conventions, such as __usercall or __userpurge.
FUNCTION_NAMEThe name of the function.
ADDRESSThe address of the function.
__VA_ARGS__The parameters of the function.

◆ GET_STATIC_HOOK_RESULT

#define GET_STATIC_HOOK_RESULT ( FUNCTION_NAME)
Value:
result_##FUNCTION_NAME

Gets the installation result of a hook defined with STATIC_HOOK, STATIC_USER_HOOK or STATIC_ASM_HOOK.

Parameters
FUNCTION_NAMEThe name of the function that was hooked.
Returns
true if the installation succeeeded. Otherwise, false.

◆ GET_STATIC_VFTABLE_HOOK_RESULT

#define GET_STATIC_VFTABLE_HOOK_RESULT ( CLASS_NAME,
FUNCTION_NAME )
Value:
result_##CLASS_NAME##_##FUNCTION_NAME

Gets the installation result of a hook defined with STATIC_VFTABLE_HOOK.

Parameters
CLASS_NAMEThe name of the class that contains the function that was hooked.
FUNCTION_NAMEThe name of the function that was hooked.
Returns
true if the installation succeeeded. Otherwise, false.

◆ HOOK

#define HOOK ( RETURN_TYPE,
CALLING_CONVENTION,
FUNCTION_NAME,
ADDRESS,
... )
Value:
typedef RETURN_TYPE CALLING_CONVENTION FUNCTION_NAME(__VA_ARGS__); \
FUNCTION_NAME* x_##FUNCTION_NAME = (FUNCTION_NAME*)(ADDRESS); \
FUNCTION_NAME* original_##FUNCTION_NAME = x_##FUNCTION_NAME; \
FUNCTION_NAME* post_##FUNCTION_NAME{}; \
RETURN_TYPE CALLING_CONVENTION impl_##FUNCTION_NAME(__VA_ARGS__)

Defines the body of a hook for a function in memory.

Parameters
RETURN_TYPEThe return type of the function.
CALLING_CONVENTIONThe calling convention of the function (e.g. __cdecl, __stdcall, __fastcall, etc). This does not support optimised calling conventions, such as __usercall or __userpurge.
FUNCTION_NAMEThe name of the function.
ADDRESSThe address of the function.
__VA_ARGS__The parameters of the function.

◆ IMPORT_FUNCTION_PTR

#define IMPORT_FUNCTION_PTR ( RETURN_TYPE,
LIBRARY_NAME,
FUNCTION_NAME,
... )
Value:
typedef RETURN_TYPE _##FUNCTION_NAME(__VA_ARGS__); \
_##FUNCTION_NAME* FUNCTION_NAME = (_##FUNCTION_NAME*)IMPORT_FUNC(LIBRARY_NAME, #FUNCTION_NAME);
#define IMPORT_FUNC(LIBRARY_NAME, FUNCTION_NAME)
Definition Preprocessor.h:3

Loads a dynamic link library into memory and declares a pointer to an exported function from it.

Parameters
RETURN_TYPEThe return type of the function.
LIBRARY_NAMEThe name of the dynamic link library.
FUNCTION_NAMEThe name of the exported function.
__VA_ARGS__The parameters of the exported function.

◆ INSTALL_HOOK

#define INSTALL_HOOK ( FUNCTION_NAME)
Value:
INSTALL_HOOK_EXPLICIT(FUNCTION_NAME, original_##FUNCTION_NAME)
#define INSTALL_HOOK_EXPLICIT(FUNCTION_NAME, ADDRESS)
Definition Hooking.h:239

Installs a hook defined with HOOK or ASM_HOOK.

Parameters
FUNCTION_NAMEThe name of the function to call before the original.
Returns
true if the installation succeeeded, or if the hook was already installed. Otherwise, false.

◆ INSTALL_HOOK_EXPLICIT

#define INSTALL_HOOK_EXPLICIT ( FUNCTION_NAME,
ADDRESS )
Value:
std::invoke([&]() \
{ \
if (!original_##FUNCTION_NAME && !(ADDRESS)) \
return false; \
\
*(void**)&original_##FUNCTION_NAME = (void*)(ADDRESS); \
\
DetourTransactionBegin(); \
DetourUpdateThread(GetCurrentThread()); \
DetourAttach((void**)&original_##FUNCTION_NAME, &impl_##FUNCTION_NAME); \
\
const auto result = DetourTransactionCommit() == NO_ERROR; \
\
if (result) \
post_##FUNCTION_NAME = hedgedev::csl::hook::GetPostHookAddress((void*)(x_##FUNCTION_NAME)); \
\
return result; \
})
void * GetPostHookAddress(void *in_pHookStart)
Definition Hooking.h:322

Installs a hook defined with HOOK or ASM_HOOK at an explicit address.

Parameters
FUNCTION_NAMEThe name of the function to call before the original.
ADDRESSThe address of the function to hook.
Returns
true if the installation succeeeded, or if the hook was already installed. Otherwise, false.

◆ INSTALL_VFTABLE_HOOK

#define INSTALL_VFTABLE_HOOK ( CLASS_NAME,
INSTANCE,
FUNCTION_NAME,
FUNCTION_INDEX )
Value:
std::invoke([&]() \
{ \
if (original_##CLASS_NAME##_##FUNCTION_NAME) \
return true; \
\
original_##CLASS_NAME##FUNCTION_NAME = (*(CLASS_NAME##_##FUNCTION_NAME***)INSTANCE)[FUNCTION_INDEX]; \
\
DetourTransactionBegin(); \
DetourUpdateThread(GetCurrentThread()); \
DetourAttach((void**)&original##CLASS_NAME##_##FUNCTION_NAME, impl_##CLASS_NAME##_##FUNCTION_NAME); \
\
const auto result = DetourTransactionCommit() == NO_ERROR; \
\
if (result) \
post_##FUNCTION_NAME = hedgedev::csl::hook::GetPostHookAddress((void*)(x_##FUNCTION_NAME)); \
\
return result; \
})

Installs a hook defined with VFTABLE_HOOK.

Parameters
CLASS_NAMEThe name of the class that contains the function being hooked.
INSTANCEA pointer to an instance of the class to extract the virtual function table pointer from.
FUNCTION_NAMEThe name of the function to call before the original.
FUNCTION_INDEXThe index of the function to hook.
Returns
true if the installation succeeeded, or if the hook was already installed. Otherwise, false.

◆ STATIC_HOOK

#define STATIC_HOOK ( RETURN_TYPE,
CALLING_CONVENTION,
FUNCTION_NAME,
ADDRESS,
... )
Value:
HOOK(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS, __VA_ARGS__); \
__CMNLIB_INTERNAL_STATIC_HOOK_IMPL(FUNCTION_NAME, ADDRESS, INSTALL_HOOK) \
RETURN_TYPE CALLING_CONVENTION impl_##FUNCTION_NAME(__VA_ARGS__)
#define INSTALL_HOOK(FUNCTION_NAME)
Definition Hooking.h:228
#define HOOK(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS,...)
Definition Hooking.h:54

Defines the body of a hook for a function in memory, and installs it upon initialisation.

Parameters
RETURN_TYPEThe return type of the function.
CALLING_CONVENTIONThe calling convention of the function (e.g. __cdecl, __stdcall, __fastcall, etc). This does not support optimised calling conventions, such as __usercall or __userpurge.
FUNCTION_NAMEThe name of the function.
ADDRESSThe address of the function.
__VA_ARGS__The parameters of the function.
Returns
Use GET_STATIC_HOOK_RESULT.

◆ STATIC_VFTABLE_HOOK

#define STATIC_VFTABLE_HOOK ( RETURN_TYPE,
CALLING_CONVENTION,
CLASS_NAME,
FUNCTION_NAME,
... )
Value:
STATIC_HOOK(RETURN_TYPE, CALLING_CONVENTION, CLASS_NAME##_##FUNCTION_NAME, nullptr, __VA_ARGS__)
#define STATIC_HOOK(RETURN_TYPE, CALLING_CONVENTION, FUNCTION_NAME, ADDRESS,...)
Definition Hooking.h:73

Defines the body of a hook for a function in a virtual function table in memory, and installs it upon initialisation.

Parameters
RETURN_TYPEThe return type of the function.
CALLING_CONVENTIONThe calling convention of the function (e.g. __cdecl, __stdcall, __fastcall, etc). This does not support optimised calling conventions, such as __usercall or __userpurge.
CLASS_NAMEThe name of the class that contains the function being hooked.
FUNCTION_NAMEThe name of the function.
__VA_ARGS__The parameters of the function.
Returns
Use GET_STATIC_VFTABLE_HOOK_RESULT.

◆ UNINSTALL_HOOK

#define UNINSTALL_HOOK ( FUNCTION_NAME)
Value:
std::invoke([&]() \
{ \
if (x_##FUNCTION_NAME == original_##FUNCTION_NAME) \
return true; \
\
DetourTransactionBegin(); \
DetourUpdateThread(GetCurrentThread()); \
DetourDetach((void**)&original_##FUNCTION_NAME, &impl_##FUNCTION_NAME); \
\
return DetourTransactionCommit() == NO_ERROR; \
})

Uninstalls a hook installed with INSTALL_HOOK.

Parameters
FUNCTION_NAMEThe name of the function to unhook.
Returns
true if the uninstallation succeeeded, or if the hook was already uninstalled. Otherwise, false.

◆ UNINSTALL_VFTABLE_HOOK

#define UNINSTALL_VFTABLE_HOOK ( CLASS_NAME,
FUNCTION_NAME )
Value:
UNINSTALL_HOOK(CLASS_NAME##_##FUNCTION_NAME)
#define UNINSTALL_HOOK(FUNCTION_NAME)
Definition Hooking.h:266

Uninstalls a hook installed with INSTALL_VFTABLE_HOOK.

Parameters
CLASS_NAMEThe name of the class that contains the function that was hooked.
FUNCTION_NAMEThe name of the function to unhook.
Returns
true if the uninstallation succeeeded, or if the hook was already uninstalled. Otherwise, false.

◆ VFTABLE_HOOK

#define VFTABLE_HOOK ( RETURN_TYPE,
CALLING_CONVENTION,
CLASS_NAME,
FUNCTION_NAME,
... )
Value:
HOOK(RETURN_TYPE, CALLING_CONVENTION, CLASS_NAME##_##FUNCTION_NAME, nullptr, __VA_ARGS__)

Defines the body of a hook for a function in a virtual function table in memory.

Parameters
RETURN_TYPEThe return type of the function.
CALLING_CONVENTIONThe calling convention of the function (e.g. __cdecl, __stdcall, __fastcall, etc). This does not support optimised calling conventions, such as __usercall or __userpurge.
CLASS_NAMEThe name of the class that contains the function being hooked.
FUNCTION_NAMEThe name of the function.
__VA_ARGS__The parameters of the function.