c++ 获取指针属于哪个模块

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);
posted @ 2021-03-09 12:08  Ajanuw  阅读(97)  评论(0编辑  收藏  举报