66#include < unordered_map>
77
88namespace {
9- std::unordered_map<intptr_t , intptr_t > g_hooksToOriginal;
9+ struct HookInfo {
10+ intptr_t target;
11+ intptr_t original;
12+ intptr_t hook;
13+ };
14+ std::unordered_map<intptr_t , HookInfo> g_hooksToOriginal;
1015}
1116
1217namespace vrperfkit {
@@ -39,15 +44,24 @@ namespace vrperfkit {
3944 return ;
4045 }
4146
42- g_hooksToOriginal[reinterpret_cast <intptr_t >(detour)] = reinterpret_cast <intptr_t >(pOriginal);
47+ g_hooksToOriginal[reinterpret_cast <intptr_t >(detour)] = HookInfo {
48+ reinterpret_cast <intptr_t >(pTarget),
49+ reinterpret_cast <intptr_t >(pOriginal),
50+ reinterpret_cast <intptr_t >(detour),
51+ };
4352 }
4453
4554 void RemoveHook (void *detour) {
4655 auto entry = g_hooksToOriginal.find (reinterpret_cast <intptr_t >(detour));
4756 if (entry != g_hooksToOriginal.end ()) {
48- void *target = reinterpret_cast <void *>(entry->second );
49- MH_DisableHook (target);
50- MH_RemoveHook (target);
57+ void *target = reinterpret_cast <void *>(entry->second .target );
58+ LOG_INFO << " Removing hook to " << target;
59+ if (MH_STATUS status; (status = MH_DisableHook (target)) != MH_OK) {
60+ LOG_ERROR << " Error when disabling hook to " << target << " : " << status;
61+ }
62+ if (MH_STATUS status; (status = MH_RemoveHook (target)) != MH_OK) {
63+ LOG_ERROR << " Error when removing hook to " << target << " : " << status;
64+ }
5165 g_hooksToOriginal.erase (entry);
5266 }
5367 }
@@ -60,7 +74,11 @@ namespace vrperfkit {
6074 return ;
6175 }
6276
63- g_hooksToOriginal[reinterpret_cast <intptr_t >(detour)] = reinterpret_cast <intptr_t >(pOriginal);
77+ g_hooksToOriginal[reinterpret_cast <intptr_t >(detour)] = HookInfo {
78+ reinterpret_cast <intptr_t >(target),
79+ reinterpret_cast <intptr_t >(pOriginal),
80+ reinterpret_cast <intptr_t >(detour),
81+ };
6482 }
6583
6684 void InstallHookInDll (const std::string &name, HMODULE module , void *detour) {
@@ -71,7 +89,7 @@ namespace vrperfkit {
7189 }
7290
7391 intptr_t HookToOriginal (intptr_t hook) {
74- return g_hooksToOriginal[hook];
92+ return g_hooksToOriginal[hook]. original ;
7593 }
7694 }
7795}
0 commit comments