MODULEINFO GetPtrModuleBase(void* ptr, DWORD pid)
{
MODULEINFO mi{ 0 };
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 me;
me.dwSize = sizeof(me);
if (Module32First(hSnap, &me))
{
do {
if (ptr >= me.modBaseAddr && ptr < me.modBaseAddr + me.modBaseSize)
{
mi.lpBaseOfDll = (LPVOID)me.modBaseAddr;
mi.SizeOfImage = me.modBaseSize;
break;
}
} while (Module32Next(hSnap, &me));
}
}
CloseHandle(hSnap);
return mi;
}
void hello(){}
MODULEINFO mi = GetPtrModuleBase(hello, GetCurrentProcessId());
printf("%p\n", mi.lpBaseOfDll);