Question

I know that in c# I can do:

Process currentProcess  = System.Diagnostics.Process.GetCurrentProcess();

currentProcess.Modules[0].BaseAddress
currentProcess.Modules[0].EntryPointAddress

How would I get this information in C++?

Taking a look I have:

void
get_module_name_for_address(LPVOID address,
                            TCHAR *buf, int buf_size)
{
    HANDLE process;
    HMODULE modules[256];
    DWORD bytes_needed, num_modules;
    unsigned int i;

    buf[0] = '\0';

    process = GetCurrentProcess();

    if (EnumProcessModules(process, (HMODULE *) &modules,
                           sizeof(modules), &bytes_needed) == 0)
    {
        return;
    }

    if (bytes_needed > sizeof(modules))
        bytes_needed = sizeof(modules);

    num_modules = bytes_needed / sizeof(HMODULE);

    for (i = 0; i < num_modules; i++)
    {
        MODULEINFO mi;

        if (GetModuleInformation(process, modules[i], &mi, sizeof(mi)) != 0)
        {
            LPVOID start, end;

            start = mi.lpBaseOfDll;
            end = (char *) start + mi.SizeOfImage;

            if (address >= start && address <= end)
            {
                GetModuleBaseName(process, modules[i], buf, buf_size);
                return;
            }
        }
    }
}
Was it helpful?

Solution

GetModuleInformation() in unmanaged code: http://msdn.microsoft.com/en-us/library/ms683201%28v=VS.85%29.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top