From 2fe65f1de98ff5f3b1afab6b33b68947b6258f47 Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 30 May 2020 19:59:20 +0600 Subject: [PATCH] Make Install(src, dst, flags) remove existing hook --- subhook.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/subhook.h b/subhook.h index f003e76..e93c332 100644 --- a/subhook.h +++ b/subhook.h @@ -186,8 +186,13 @@ class Hook { bool Install(void *src, void *dst, HookFlags flags = HookNoFlags) { + if (hook_ != nullptr) { + subhook_remove(hook_); + subhook_free(hook_); + } + hook_ = subhook_new(src, dst, (subhook_flags_t)flags); if (hook_ == nullptr) { - hook_ = subhook_new(src, dst, (subhook_flags_t)flags); + return false; } return Install(); } @@ -211,8 +216,8 @@ class Hook { class ScopedHookRemove { public: ScopedHookRemove(Hook *hook) - : hook_(hook) - , removed_(hook_->Remove()) + : hook_(hook), + removed_(hook_->Remove()) { } @@ -234,8 +239,8 @@ class ScopedHookRemove { class ScopedHookInstall { public: ScopedHookInstall(Hook *hook) - : hook_(hook) - , installed_(hook_->Install()) + : hook_(hook), + installed_(hook_->Install()) { } @@ -243,8 +248,8 @@ class ScopedHookInstall { void *src, void *dst, HookFlags flags = HookNoFlags) - : hook_(hook) - , installed_(hook_->Install(src, dst, flags)) + : hook_(hook), + installed_(hook_->Install(src, dst, flags)) { }