C++ Get 64bit Process Base Address

  64-bit, base, c++, memory, windows

I have been trying to get the base address of a 64bit process. (Win 10)

I looked up everywhere and tried all of the codes was provided but unfortunately doesn’t work any of them.

One of my tries:

#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <psapi.h>
#include <iostream>
#include <tlhelp32.h>

uintptr_t GetModuleBaseAddress(DWORD procId, const wchar_t* modName)
{
    uintptr_t modBaseAddr = 0;
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
    if (hSnap != INVALID_HANDLE_VALUE)
    {
        MODULEENTRY32 modEntry;
        modEntry.dwSize = sizeof(modEntry);
        if (Module32First(hSnap, &modEntry))
        {
            do
            {
                if (!_wcsicmp(modEntry.szModule, modName))
                {
                    modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
                    break;
                }
            } while (Module32Next(hSnap, &modEntry));
        }
    }
    CloseHandle(hSnap);
    return modBaseAddr;
}

int main()
{
    DWORD64 Base = GetModuleBaseAddress((DWORD)27976, L"Calculator.exe");
    printf("0x%x", Base);
}

The code always gives 0 (if it doesn’t have an error) as the base address.

Source: Windows Questions

LEAVE A COMMENT