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

Get number of modules of a running process

本文关键字:进程 模块 运行 获取      更新时间:2023-10-16

我试图通过传递进程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;
}

当我编译和运行时,此代码输出855987977等 我尝试了多种方法,但都是徒劳的...

使用 EnumprocessModule 的标准方法,输出参数是数组的大小(以字节为单位(:

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: %un", 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;
}