aboutsummaryrefslogtreecommitdiff
path: root/VmtHook.cpp
diff options
context:
space:
mode:
authorBDKPlayer <fabian.stotz@yahoo.de>2020-04-01 20:57:35 +0200
committerBDKPlayer <fabian.stotz@yahoo.de>2020-04-01 20:57:35 +0200
commitd0f91aed16a1cb40df6e1ab7c19b17410888906d (patch)
tree401f2a4d5690b03f22e11212832dbe1693404536 /VmtHook.cpp
parent1d8b290947b6b94f75eebc72a6becf6278729925 (diff)
Removed detours dependency
Diffstat (limited to 'VmtHook.cpp')
-rw-r--r--VmtHook.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/VmtHook.cpp b/VmtHook.cpp
new file mode 100644
index 0000000..915e82f
--- /dev/null
+++ b/VmtHook.cpp
@@ -0,0 +1,51 @@
+#include "VmtHook.h"
+
+
+
+VmtHook::VmtHook(void** vmt)
+{
+ this->vmt = vmt;
+}
+
+void* VmtHook::Hook(int index, void* hookedFunction)
+{
+ hookedfuncs.insert(std::make_pair(index, vmt[index]));
+
+ //make page writeable
+ DWORD pageProtection;
+ VirtualProtect(&vmt[index], sizeof(BYTE*), PAGE_EXECUTE_READWRITE, &pageProtection);
+
+ //overwrite function pointer in VMT to hook function
+ vmt[index] = hookedFunction;
+
+ //restore page protection
+ VirtualProtect(&vmt[index], sizeof(BYTE*), pageProtection, &pageProtection);
+
+ return hookedfuncs[index];
+}
+
+//
+//Unhook a single hooked function
+bool VmtHook::Unhook(int index)
+{
+ auto entry = hookedfuncs.find(index);
+ if (entry != hookedfuncs.end())
+ {
+ vmt[entry->first] = entry->second;
+ return true;
+ }
+ return false;
+}
+
+//
+//Unhook the entire Vmt
+void VmtHook::Unhook()
+{
+ for (std::pair<int, void*> pair : hookedfuncs)
+ {
+ DWORD oldProtection;
+ VirtualProtect(&vmt[pair.first], sizeof(BYTE*), PAGE_EXECUTE_READWRITE, &oldProtection);
+ vmt[pair.first] = pair.second;
+ VirtualProtect(&vmt[pair.first], sizeof(BYTE*), oldProtection, &oldProtection);
+ }
+} \ No newline at end of file