HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Hook.h
Go to the documentation of this file.
1
15
16/*
17MIT License
18
19Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
20
21Permission is hereby granted, free of charge, to any person obtaining a copy
22of this software and associated documentation files (the "Software"), to deal
23in the Software without restriction, including without limitation the rights
24to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25copies of the Software, and to permit persons to whom the Software is
26furnished to do so, subject to the following conditions:
27
28The above copyright notice and this permission notice shall be included in all
29copies or substantial portions of the Software.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37SOFTWARE.
38*/
39
40#pragma once
41
42#include "Windows.h"
43#include <detours/detours.h>
44#include "Exceptions.hpp"
45
54
61
63template <CallConvention cc, typename retn, typename convention, typename ...args>
65
66template <typename retn, typename ...args>
67struct convention<CallConvention::stdcall_t, retn, args...>
68{
69 typedef retn (__stdcall *type)(args ...);
70};
71
72template <typename retn, typename ...args>
73struct convention<CallConvention::cdecl_t, retn, args...>
74{
75 typedef retn (__cdecl *type)(args ...);
76};
77
84template <CallConvention cc, typename retn, typename ...args>
85class 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 {
107 "A pending transaction already exists"
108 );
109 }
110
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:
137 "Target function was changed by third party between steps of the transaction"
138 );
139
140 case ERROR_INVALID_OPERATION:
142 "No pending transaction exists"
143 );
144
145 case ERROR_INVALID_BLOCK:
147 "The function referenced is too small to be detoured"
148 );
149
150 case ERROR_INVALID_HANDLE:
152 "The ppPointer parameter is null or points to a null pointer"
153 );
154
155 case ERROR_NOT_ENOUGH_MEMORY:
157 "Not enough memory exists to complete the operation"
158 );
159
160 default:
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 {
177 "Not enough memory to record identity of thread"
178 );
179 }
180
182 }
183 }
184
185public:
187 Hook() : orig_(0), detour_(0), is_applied_(false), has_open_transaction_(false)
188 {
189 }
190
192 ~Hook() noexcept
193 {
195 return;
196
197 abort_transaction_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:
230 "The function referenced is too small to be detoured"
231 );
232
233 case ERROR_INVALID_HANDLE:
235 "The ppPointer parameter is null or points to a null pointer"
236 );
237
238 case ERROR_INVALID_OPERATION:
240 "No pending transaction exists"
241 );
242
243 case ERROR_NOT_ENOUGH_MEMORY:
245 "Not enough memory exists to complete the operation"
246 );
247
248 default:
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:
282 "The function to be detached was too small to be detoured"
283 );
284
285 case ERROR_INVALID_HANDLE:
287 "The ppPointer parameter is null or points to a null pointer"
288 );
289
290 case ERROR_INVALID_OPERATION:
292 "No pending transaction exists"
293 );
294
295 case ERROR_NOT_ENOUGH_MEMORY:
297 "Not enough memory exists to complete the operation"
298 );
299
300 default:
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};
HydraHook exception types (Detours, Win32, generic).
CallConvention
Supported calling conventions for hook targets.
Definition Hook.h:57
bool g_hookDestructorDetachSuppressed
When set, ~Hook() does not detach.
Definition Hook.h:53
void apply(T pFunc, type detour)
Attaches detour to target function.
Definition Hook.h:208
~Hook() noexcept
Destroys hook; removes if applied. Never throws.
Definition Hook.h:192
retn call_orig(args ... p)
Calls the original (unhooked) function.
Definition Hook.h:354
void remove()
Removes hook if applied.
Definition Hook.h:265
bool is_applied() const
Returns true if hook is currently applied.
Definition Hook.h:360
bool remove_nothrow() noexcept
Removes hook if applied. Never throws; safe to call under loader lock.
Definition Hook.h:321
type original() const
Trampoline while applied; otherwise the address last passed to apply.
Definition Hook.h:374
Hook()
Constructs an unapplied hook.
Definition Hook.h:187
Maps CallConvention to function pointer type.
Definition Hook.h:64