-
Notifications
You must be signed in to change notification settings - Fork 1
/
CallHook.cpp
32 lines (23 loc) · 854 Bytes
/
CallHook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "memory_patch.h"
#include <assert.h>
#include <stdexcept>
#include <subhook.h>
extern "C" void* subhook_unprotect(void* address, size_t size);
namespace memory_patch {
constexpr auto CallHookVOffset = sizeof(uintptr_t) + 1;
CallHookV::CallHookV(Address addr, const void* func, void* expect) :
Base(reinterpret_cast<void*>(addr), CallHookVOffset, sizeof(uintptr_t), expect), m_modified(func)
{
if (nullptr == subhook_unprotect(reinterpret_cast<void*>(addr), CallHookVOffset))
throw std::runtime_error("subhook_unprotect failed");
// I'm sure a kitten died somewhere, but at least it died on all
// x86 systems at once.
m_original = *(Address*)(addr + 1);
*(Address*)(addr + 1) = (Address) m_modified - (addr + CallHookVOffset);
}
CallHookV::~CallHookV()
{
assert(m_original);
*(Address*)((Address)m_addr + 1) = m_original;
}
}