Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <stdint.h>
- #include <Windows.h>
- namespace asmjs
- {
- class VMTHook
- {
- static int Unprotect(void* region)
- {
- MEMORY_BASIC_INFORMATION mbi;
- VirtualQuery((LPCVOID)region, &mbi, sizeof(mbi));
- VirtualProtect(mbi.BaseAddress, mbi.RegionSize, PAGE_READWRITE, &mbi.Protect);
- return mbi.Protect;
- }
- static void Protect(void* region, int protection)
- {
- MEMORY_BASIC_INFORMATION mbi;
- VirtualQuery((LPCVOID)region, &mbi, sizeof(mbi));
- VirtualProtect(mbi.BaseAddress, mbi.RegionSize, protection, &mbi.Protect);
- }
- public:
- /*
- * pObjectInstance: pointer to an pObjectInstance of a class
- * hookFn: function to overwrite with
- * methodOffset: 0 = method 1, 1 = method 2 etc...
- * return: original function
- */
- static void* Install(void* pObjectInstance, void* hookFn, int methodOffset)
- {
- intptr_t vtable = *((intptr_t*)pObjectInstance);
- intptr_t entry = vtable + sizeof(intptr_t) * methodOffset;
- intptr_t original = *((intptr_t*)entry);
- int originalProtection = Unprotect((void*)entry);
- *((intptr_t*)entry) = (intptr_t)hookFn;
- Protect((void*)entry, originalProtection);
- return (void*)original;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement