HydraHook
Direct3D and Core Audio hooking library for Windows
Toggle main menu visibility
Loading...
Searching...
No Matches
Hook.h
Go to the documentation of this file.
1
15
16
/*
17
MIT License
18
19
Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
20
21
Permission is hereby granted, free of charge, to any person obtaining a copy
22
of this software and associated documentation files (the "Software"), to deal
23
in the Software without restriction, including without limitation the rights
24
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
copies of the Software, and to permit persons to whom the Software is
26
furnished to do so, subject to the following conditions:
27
28
The above copyright notice and this permission notice shall be included in all
29
copies or substantial portions of the Software.
30
31
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
SOFTWARE.
38
*/
39
40
#pragma once
41
42
#include "Windows.h"
43
#include <detours/detours.h>
44
#include "
Exceptions.hpp
"
45
53
inline
bool
g_hookDestructorDetachSuppressed
=
false
;
54
56
enum class
CallConvention
57
{
58
stdcall_t
,
59
cdecl_t
60
};
61
63
template
<
CallConvention
cc,
typename
retn,
typename
convention
,
typename
...args>
64
struct
convention
;
65
66
template
<
typename
retn,
typename
...args>
67
struct
convention
<
CallConvention
::
stdcall_t
, retn, args...>
68
{
69
typedef
retn (__stdcall *
type
)(args ...);
70
};
71
72
template
<
typename
retn,
typename
...args>
73
struct
convention
<
CallConvention
::
cdecl_t
, retn, args...>
74
{
75
typedef
retn (__cdecl *
type
)(args ...);
76
};
77
84
template
<
CallConvention
cc,
typename
retn,
typename
...args>
85
class
Hook
86
{
87
typedef
typename
convention
<cc, retn, args...>::type type;
88
89
size_t
orig_;
90
type detour_;
91
92
bool
is_applied_;
93
bool
has_open_transaction_;
94
95
Hook
(
const
Hook&) =
delete
;
96
Hook& operator=(
const
Hook&) =
delete
;
97
98
void
transaction_begin()
99
{
100
const
auto
result = DetourTransactionBegin();
101
102
if
(result != NO_ERROR)
103
{
104
if
(result == ERROR_INVALID_OPERATION)
105
{
106
throw
HydraHook::Core::Exceptions::DetourException
(
107
"A pending transaction already exists"
108
);
109
}
110
111
throw
HydraHook::Core::Exceptions::DetourException
(
"Unknown error"
);
112
}
113
114
has_open_transaction_ =
true
;
115
}
116
117
void
abort_transaction_nothrow()
noexcept
118
{
119
if
(!has_open_transaction_)
120
return
;
121
122
DetourTransactionAbort();
123
has_open_transaction_ =
false
;
124
}
125
126
void
transaction_commit()
127
{
128
const
auto
result = DetourTransactionCommit();
129
has_open_transaction_ =
false
;
130
131
if
(result != NO_ERROR)
132
{
133
switch
(result)
134
{
135
case
ERROR_INVALID_DATA:
136
throw
HydraHook::Core::Exceptions::DetourException
(
137
"Target function was changed by third party between steps of the transaction"
138
);
139
140
case
ERROR_INVALID_OPERATION:
141
throw
HydraHook::Core::Exceptions::DetourException
(
142
"No pending transaction exists"
143
);
144
145
case
ERROR_INVALID_BLOCK:
146
throw
HydraHook::Core::Exceptions::DetourException
(
147
"The function referenced is too small to be detoured"
148
);
149
150
case
ERROR_INVALID_HANDLE:
151
throw
HydraHook::Core::Exceptions::DetourException
(
152
"The ppPointer parameter is null or points to a null pointer"
153
);
154
155
case
ERROR_NOT_ENOUGH_MEMORY:
156
throw
HydraHook::Core::Exceptions::DetourException
(
157
"Not enough memory exists to complete the operation"
158
);
159
160
default
:
161
throw
HydraHook::Core::Exceptions::DetourException
(
162
"Unknown error"
163
);
164
}
165
}
166
}
167
168
static
void
update_thread(HANDLE hThread)
169
{
170
const
auto
result = DetourUpdateThread(hThread);
171
172
if
(result != NO_ERROR)
173
{
174
if
(result == ERROR_NOT_ENOUGH_MEMORY)
175
{
176
throw
HydraHook::Core::Exceptions::DetourException
(
177
"Not enough memory to record identity of thread"
178
);
179
}
180
181
throw
HydraHook::Core::Exceptions::DetourException
(
"Unknown error"
);
182
}
183
}
184
185
public
:
187
Hook
() : orig_(0), detour_(0), is_applied_(false), has_open_transaction_(false)
188
{
189
}
190
192
~Hook
() noexcept
193
{
194
if
(
g_hookDestructorDetachSuppressed
)
195
return
;
196
197
abort_transaction_nothrow();
198
remove_nothrow
();
199
}
200
207
template
<
typename
T>
208
void
apply
(T pFunc, type detour)
209
{
210
if
(is_applied_)
211
{
212
throw
HydraHook::Core::Exceptions::DetourException
(
"Hook is already applied"
);
213
}
214
215
detour_ = detour;
216
orig_ =
static_cast<
size_t
>
(pFunc);
217
218
transaction_begin();
219
try
220
{
221
update_thread(GetCurrentThread());
222
const
auto
result = DetourAttach(
reinterpret_cast<
void
**
>
(&orig_),
reinterpret_cast<
void
*
>
(detour_));
223
224
if
(result != NO_ERROR)
225
{
226
switch
(result)
227
{
228
case
ERROR_INVALID_BLOCK:
229
throw
HydraHook::Core::Exceptions::DetourException
(
230
"The function referenced is too small to be detoured"
231
);
232
233
case
ERROR_INVALID_HANDLE:
234
throw
HydraHook::Core::Exceptions::DetourException
(
235
"The ppPointer parameter is null or points to a null pointer"
236
);
237
238
case
ERROR_INVALID_OPERATION:
239
throw
HydraHook::Core::Exceptions::DetourException
(
240
"No pending transaction exists"
241
);
242
243
case
ERROR_NOT_ENOUGH_MEMORY:
244
throw
HydraHook::Core::Exceptions::DetourException
(
245
"Not enough memory exists to complete the operation"
246
);
247
248
default
:
249
throw
HydraHook::Core::Exceptions::DetourException
(
"Unknown error"
);
250
}
251
}
252
253
transaction_commit();
254
}
255
catch
(...)
256
{
257
abort_transaction_nothrow();
258
throw
;
259
}
260
261
is_applied_ =
true
;
262
}
263
265
void
remove
()
266
{
267
if
(!is_applied_)
268
return
;
269
270
transaction_begin();
271
try
272
{
273
update_thread(GetCurrentThread());
274
const
auto
result = DetourDetach(
reinterpret_cast<
void
**
>
(&orig_),
reinterpret_cast<
void
*
>
(detour_));
275
276
if
(result != NO_ERROR)
277
{
278
switch
(result)
279
{
280
case
ERROR_INVALID_BLOCK:
281
throw
HydraHook::Core::Exceptions::DetourException
(
282
"The function to be detached was too small to be detoured"
283
);
284
285
case
ERROR_INVALID_HANDLE:
286
throw
HydraHook::Core::Exceptions::DetourException
(
287
"The ppPointer parameter is null or points to a null pointer"
288
);
289
290
case
ERROR_INVALID_OPERATION:
291
throw
HydraHook::Core::Exceptions::DetourException
(
292
"No pending transaction exists"
293
);
294
295
case
ERROR_NOT_ENOUGH_MEMORY:
296
throw
HydraHook::Core::Exceptions::DetourException
(
297
"Not enough memory exists to complete the operation"
298
);
299
300
default
:
301
throw
HydraHook::Core::Exceptions::DetourException
(
302
"Unknown error"
303
);
304
}
305
}
306
307
transaction_commit();
308
is_applied_ =
false
;
309
}
310
catch
(...)
311
{
312
abort_transaction_nothrow();
313
throw
;
314
}
315
}
316
321
bool
remove_nothrow
() noexcept
322
{
323
if
(!is_applied_)
324
return
true
;
325
326
if
(DetourTransactionBegin() != NO_ERROR)
327
return
false
;
328
329
if
(DetourUpdateThread(GetCurrentThread()) != NO_ERROR)
330
{
331
DetourTransactionAbort();
332
return
false
;
333
}
334
335
const
auto
result = DetourDetach(
reinterpret_cast<
void
**
>
(&orig_),
reinterpret_cast<
void
*
>
(detour_));
336
337
if
(result != NO_ERROR)
338
{
339
DetourTransactionAbort();
340
return
false
;
341
}
342
343
if
(DetourTransactionCommit() != NO_ERROR)
344
{
345
DetourTransactionAbort();
346
return
false
;
347
}
348
349
is_applied_ =
false
;
350
return
true
;
351
}
352
354
retn
call_orig
(args ... p)
355
{
356
return
type(orig_)(p...);
357
}
358
360
bool
is_applied
()
const
361
{
362
return
is_applied_;
363
}
364
374
type
original
()
const
375
{
376
return
reinterpret_cast<
type
>
(orig_);
377
}
378
};
Exceptions.hpp
HydraHook exception types (Detours, Win32, generic).
CallConvention
CallConvention
Supported calling conventions for hook targets.
Definition
Hook.h:57
CallConvention::cdecl_t
@ cdecl_t
Definition
Hook.h:59
CallConvention::stdcall_t
@ stdcall_t
Definition
Hook.h:58
g_hookDestructorDetachSuppressed
bool g_hookDestructorDetachSuppressed
When set, ~Hook() does not detach.
Definition
Hook.h:53
Hook::apply
void apply(T pFunc, type detour)
Attaches detour to target function.
Definition
Hook.h:208
Hook::~Hook
~Hook() noexcept
Destroys hook; removes if applied. Never throws.
Definition
Hook.h:192
Hook::call_orig
retn call_orig(args ... p)
Calls the original (unhooked) function.
Definition
Hook.h:354
Hook::remove
void remove()
Removes hook if applied.
Definition
Hook.h:265
Hook::is_applied
bool is_applied() const
Returns true if hook is currently applied.
Definition
Hook.h:360
Hook::remove_nothrow
bool remove_nothrow() noexcept
Removes hook if applied. Never throws; safe to call under loader lock.
Definition
Hook.h:321
Hook::original
type original() const
Trampoline while applied; otherwise the address last passed to apply.
Definition
Hook.h:374
Hook::Hook
Hook()
Constructs an unapplied hook.
Definition
Hook.h:187
HydraHook::Core::Exceptions::DetourException
Definition
Exceptions.hpp:31
convention< CallConvention::cdecl_t, retn, args... >::type
retn(__cdecl *) type(args ...)
Definition
Hook.h:75
convention< CallConvention::stdcall_t, retn, args... >::type
retn(__stdcall *) type(args ...)
Definition
Hook.h:69
convention
Maps CallConvention to function pointer type.
Definition
Hook.h:64
src
HydraHook
Utils
Hook.h
Generated by
1.18.0