-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetours_api.h
More file actions
150 lines (106 loc) · 5.41 KB
/
detours_api.h
File metadata and controls
150 lines (106 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copypasta from SvenMod
#ifndef DETOURS_API_H
#define DETOURS_API_H
#ifdef _WIN32
#pragma once
#endif
#include <vector>
#include "hashtable.h"
#include "memory_utils.h"
//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------
#define FUNC_SIGNATURE(retType, callConv, typeName, ...) typedef retType(callConv *typeName)(__VA_ARGS__)
#define DECLARE_FUNC_PTR(retType, callConv, funcName, ...) typedef retType(callConv *(funcName##Fn))(__VA_ARGS__); funcName##Fn funcName = 0
#define DECLARE_FUNC(retType, callConv, funcName, ...) retType callConv funcName(__VA_ARGS__)
#define DECLARE_CLASS_FUNC(retType, funcName, thisPtr, ...) retType __fastcall funcName(thisPtr, void *edx, __VA_ARGS__)
#define GET_FUNC_PTR(funcName) (void **)&funcName
#define DECLARE_HOOK(retType, callConv, funcName, ...) \
typedef retType(callConv *funcName##Fn)(__VA_ARGS__); \
static retType callConv HOOKED_##funcName(__VA_ARGS__); \
funcName##Fn ORIG_##funcName = 0
#define DECLARE_CLASS_HOOK(retType, funcName, thisPtr, ...) \
typedef retType(__thiscall *funcName##Fn)(thisPtr, __VA_ARGS__); \
static retType __fastcall HOOKED_##funcName(thisPtr, void *edx, __VA_ARGS__); \
funcName##Fn ORIG_##funcName = 0
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CDetourFunction;
class CDetourContext;
//-----------------------------------------------------------------------------
// Detour handle
//-----------------------------------------------------------------------------
typedef int DetourHandle_t;
#define DETOUR_INVALID_HANDLE (DetourHandle_t)(-1)
//-----------------------------------------------------------------------------
// Type
//-----------------------------------------------------------------------------
typedef enum
{
DETOUR_FUNCTION = 0,
DETOUR_VTABLE_FUNCTION,
DETOUR_IAT_FUNCTION
} DETOUR_TYPE;
//-----------------------------------------------------------------------------
// CDetoursAPI
//-----------------------------------------------------------------------------
class CDetoursAPI
{
friend class CDetourContext;
public:
CDetoursAPI();
~CDetoursAPI();
void Init();
//-----------------------------------------------------------------------------
// Hook function
//
// @param ppOriginalFunction - pointer to pointer to original function to call
// @param iDisasmMinBytes - minimum bytes to steal from original function, it
// can be helpful if detour's trampoline crashes (i.e. jump-short opcode causes it)
// ToDo: fix it..
//-----------------------------------------------------------------------------
DetourHandle_t DetourFunction(void *pFunction, void *pDetourFunction, void **ppOriginalFunction, bool bPause = false, int iDisasmMinBytes = 5);
//-----------------------------------------------------------------------------
// Find function from import address table (using GetProcAddress) and hook it
//-----------------------------------------------------------------------------
DetourHandle_t DetourFunctionByName(HMODULE hModule, const char *pszFunctionName, void *pDetourFunction, void **ppOriginalFunction, bool bPause = false, int iDisasmMinBytes = 5);
//-----------------------------------------------------------------------------
// Hook function from virtual methods table
//-----------------------------------------------------------------------------
DetourHandle_t DetourVirtualFunction(void *pClassInstance, int nFunctionIndex, void *pDetourFunction, void **ppOriginalFunction, bool bPause = false);
//-----------------------------------------------------------------------------
// Pause function. Returns true if success
//-----------------------------------------------------------------------------
bool PauseDetour(DetourHandle_t hDetour);
//-----------------------------------------------------------------------------
// Unpause function. Returns true if success
//-----------------------------------------------------------------------------
bool UnpauseDetour(DetourHandle_t hDetour);
//-----------------------------------------------------------------------------
// Remove/unhook function
//-----------------------------------------------------------------------------
bool RemoveDetour(DetourHandle_t hDetour);
//-----------------------------------------------------------------------------
// Pause all registered detours
//-----------------------------------------------------------------------------
bool PauseAllDetours();
//-----------------------------------------------------------------------------
// Unpause all registered detours
//-----------------------------------------------------------------------------
bool UnpauseAllDetours();
private:
DetourHandle_t CreateDetour(DETOUR_TYPE type, void *pFunction, void *pDetourFunction, void **ppOriginalFunction, bool bPause, int iDisasmMinBytes);
DetourHandle_t AllocateDetourHandle();
void SuspendThreads();
void ResumeThreads();
CHashTable<DetourHandle_t, CDetourFunction *> m_DetoursTable;
CHashTable<void *, CDetourContext *> m_ContextsTable;
std::vector<unsigned long> m_SuspendedThreads;
unsigned long m_dwCurrentThreadID;
unsigned long m_dwCurrentProcessID;
int m_iDetourHandles;
};
extern CDetoursAPI g_DetoursAPI;
CDetoursAPI *DetoursAPI();
#endif // DETOURS_API_H