获取正在运行的进程的模块数

时间:2018-06-08 20:39:12

标签: c++ windows

我试图通过传递进程ID来获取正在运行的进程的模块总数 这是返回流程中模块总数的函数

int size(DWORD processID)
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
// Print the process identifier.
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
// Get a list of all the modules in this process.

EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded);
int j = (cbNeeded / sizeof(HMODULE));

return j;


// Release the handle to the process.

}

这是主要的

int main()
{
    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 (int i = 0; i <= cProcesses; i++) {
        int a = size(aProcesses[1]);
        //std::string* g = PrintModules(aProcesses[1], a);
        cout << a << endl;
    }

    system("pause");
    return 0;
}

当我编译&amp;运行此代码输出是855987977等 我尝试了多种方法但都徒劳无功......

1 个答案:

答案 0 :(得分:0)

使用EnumprocessModules的标准方法,输出参数是数组的字节大小:

lpcbNeeded = The number of bytes required to store all module handles in the lphModule array.

将其除以元素类型(HMODULE)的大小,这将产生模块数量。

int GetNumberOfModules(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))
    {
        //return number of modules by dividing size of array by element size
        return cbNeeded / sizeof(HMODULE);
    }

    // Release the handle to the process.

    CloseHandle(hProcess);

    return 0;
}