windows 获取进程全部模块

最一开始还找到了Module32First,Module32Next 这两个函数,但是这两个函数只能获取32位的模块或64位的模块。

使用 获取所有模块的官方例子:https://docs.microsoft.com/en-us/windows/win32/toolhelp/traversing-the-module-list

Module32First 函数官方说明文档:https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-module32first

Module32Next 函数官方说明文档:https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-module32next

枚举一个进程所有加载的模块官方例子:https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-modules-for-a-process

EnumProcessModules 官方说明文档:https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodules

EnumProcessModulesEx 官方说明文档:https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex

那个官方的枚举教程中的例子是用的 EnumProcessModules 这个函数,但是这个函数只会枚举出来所有32位的模块,或者64位的模块。

而使用 EnumProcessModulesEx 这个函数,就可以同时获取32位的和64位的模块,或者分开获取。

用ce时,你可以在内存扫描选项那里找到该进程的所有模块。

样例:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <psapi.h>

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

int PrintModules(DWORD processID)
{
    HMODULE hMods[1024];
    HANDLE hProcess;
    DWORD cbNeeded;
    unsigned int i;

    // Print the process identifier.

    printf("\nProcess ID: %u\n", processID);

    // Get a handle to the process.

    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
        PROCESS_VM_READ,
        FALSE, processID);
    if (NULL == hProcess)
        return 1;

    // Get a list of all the modules in this process.

    //if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
    //if (EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, LIST_MODULES_32BIT))
    //if (EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, LIST_MODULES_64BIT))
    if (EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, LIST_MODULES_ALL))
    {
        for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
        {
            TCHAR szModName[MAX_PATH];

            // Get the full path to the module's file.

            if (GetModuleFileNameEx(hProcess, hMods[i], szModName,
                sizeof(szModName) / sizeof(TCHAR)))
            {
                // Print the module name and handle value.

                _tprintf(TEXT("\t%s (0x%08X)\n"), szModName, hMods[i]);
            }
        }
    }

    // Release the handle to the process.

    CloseHandle(hProcess);

    return 0;
}

int main(void)
{

    DWORD aProcesses[1024];
    DWORD cbNeeded;
    DWORD cProcesses;
    unsigned int i;

    // Get the list of process identifiers.

    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
        return 1;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the names of the modules for each process.

    //for (i = 0; i < cProcesses; i++)
    //{
    //    printf("%d\n", aProcesses[i]);
    //    //PrintModules(aProcesses[i]);
    //}

    PrintModules(15252);

    return 0;
}
文章目录