This repository has been archived on 2026-11-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
my-first-apex-internal/shd/vmt.cpp
T
2026-11-05 02:19:09 +01:00

46 lines
1.1 KiB
C++

#include <include.h>
size_t c_vfunc_hook::estimate_vftbl_length(std::uintptr_t* vftbl_start)
{
MEMORY_BASIC_INFORMATION memInfo = {};
int m_nSize = -1;
do {
m_nSize++;
win_api::rebuilt::virtual_query(reinterpret_cast<LPCVOID>(vftbl_start[m_nSize]), &memInfo, sizeof(memInfo));
} while (memInfo.Protect == PAGE_EXECUTE_READ || memInfo.Protect == PAGE_EXECUTE_READWRITE);
return m_nSize;
}
bool c_vfunc_hook::setup(void* base)
{
if (base != nullptr)
class_base = base;
if (class_base == nullptr)
return false;
old_vftbl = *static_cast<std::uintptr_t**>(class_base);
vftbl_len = estimate_vftbl_length(old_vftbl) * sizeof(std::uintptr_t);
if (vftbl_len == 0)
return false;
new_vftb1 = stl::mem_alloc<uintptr_t*>((sizeof(uintptr_t) * (vftbl_len + 1)));
if (new_vftb1 == nullptr)
return false;
memcpy(&new_vftb1[1], old_vftbl, vftbl_len);
auto guard = detail::protect_guard{ class_base, sizeof(std::uintptr_t), PAGE_READWRITE };
new_vftb1[0] = old_vftbl[-1];
*static_cast<std::uintptr_t**>(class_base) = &new_vftb1[1];
wasAllocated = true;
return true;
}