我正在尝试获取给定进程正在使用的 DLL 列表,我正在尝试通过 VirtualQueryEx 来实现。我的问题是它返回给我的只是 DLL 的部分列表,而不是全部(我可以使用 Process Explorer 或在给定进程上使用 VirtualQuery 来查看该列表)。
这是代码:
char szBuf[MAX_PATH * 100] = { 0 };
PBYTE pb = NULL;
MEMORY_BASIC_INFORMATION mbi;
HANDLE h_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, iPID);
while (VirtualQueryEx(h_process, pb, &mbi, sizeof(mbi)) == sizeof(mbi)) {
int nLen;
char szModName[MAX_PATH];
if (mbi.State == MEM_FREE)
mbi.AllocationBase = mbi.BaseAddress;
if ((mbi.AllocationBase == hInstDll) ||
(mbi.AllocationBase != mbi.BaseAddress) ||
(mbi.AllocationBase == NULL)) {
// Do not add the module name to the list
// if any of the following is true:
// 1. If this region contains this DLL
// 2. If this block is NOT the beginning of a region
// 3. If the address is NULL
nLen = 0;
} else {
nLen = GetModuleFileNameA((HINSTANCE) mbi.AllocationBase,
szModName, _countof(szModName));
}
if (nLen > 0) {
wsprintfA(strchr(szBuf, 0), "\n%p-%s",
mbi.AllocationBase, szModName);
}
pb += mbi.RegionSize;
}
我正在得到结果szBuf
。
此函数是 DLL 文件的一部分,因此我很难调试。
现在 DLL 被编译为 x64 二进制文件,我将它用于 x64 进程。
PS 我知道 EnumProcessModules 并且我没有使用它是有原因的(太长了:)。