http://bbs.s-sos.net/viewthread.php?tid=5630
DLL获取自己的模块句柄的方法
HMODULE GetSelfModuleHandle()
{
MEMORY_BASIC_INFORMATION mbi;
return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
? (HMODULE) mbi.AllocationBase : NULL);
}
这几天看了一下window核心编程,第22章有一个例子使用远程调用注入Dll的。其中注入Dll的时候载入dll的进程调用VirtualQuery查询进程虚拟空间得到进程载入的所有模块路径。
但是,查询代码很奇怪,于是翻看文档,VirtualQuery说明中没有说到过任何与dll有关的话题,但作者又如何肯定可以读出dll的信息呢?看来值得深入研究一下。
首先看一下代码:
- #include <Windows.h>
- #include <fstream>
- #include <tchar.h>
- int WINAPI DllMain(HINSTANCE hDllHandle, DWORD dwReason, LPVOID lpReserved)
- {
- if (dwReason == DLL_PROCESS_ATTACH)
- {
- std::wfstream file;
- file.open(_T("D://SeeIn.txt"), std::ios_base::out);
- if (!file)
- {
- return FALSE;
- }
- MEMORY_BASIC_INFORMATION memory;
- memset(&memory, 0, sizeof(memory));
- DWORD pFind = NULL;
- while(VirtualQuery((LPVOID)pFind, &memory, sizeof(memory)) == sizeof(memory))
- {
- if (memory.State == MEM_FREE)
- {
- memory.AllocationBase = memory.BaseAddress;
- }
- if (memory.BaseAddress == hDllHandle ||
- memory.AllocationBase != memory.BaseAddress ||
- memory.AllocationBase == 0)
- {
- pFind += memory.RegionSize;
- memset(&memory, 0, sizeof(memory));
- continue;
- }
- else
- {
- TCHAR szName[MAX_PATH] = {0};
- if (0 != GetModuleFileName((HMODULE)memory.AllocationBase, szName, sizeof(szName) / sizeof(TCHAR)))
- {
- file.write(szName, sizeof(szName));
- //file.write(_T("/r/n"), sizeof(_T("/r/n")));
- file.clear();
- }
- }
- pFind += memory.RegionSize;
- memset(&memory, 0, sizeof(memory));
- }
- file.close();
- }
- return TRUE;
- }
其中可以看出,AllocationBase == BaseAddress的时候,这个页是一个dll模块载入进来的时候分配的,查看文档,对于AllocationBase 是这么说的:
A pointer to the base address of a range of pages allocated by the VirtualAlloc function. The page pointed to by the BaseAddress member is contained within this allocation range.
翻译:一个指向一系列范围内的页的基地址的指针,这些页是通过VirtualAlloc 函数分配的。被BaseAddress 成员指向的页被包含在这个分配范围内。
通过代码和文档,看一看出,AllocationBase 是页的基地址,BaseAddress 也是页的基地址,唯一不同的是,BaseAddress 是VirtualAlloc 分配内存后第一页的地址,AllocationBase 是每一页的地址。
比如假设windows为每一块由VirtualAlloc 分配的内存准备了若干页:p1,p2,p3,p4,……那么,BaseAddress 就是p1的基地址,而AllocationBase 就分别是p1,p2,p3,p4,……的基地址。
当然,要想获得每一个页的AllocationBase ,那就必须不断的调用VirtualQuery遍历这些页。
还有一个重要的地方,从这个程序看出,进程载入dll内存空间是由VirtualAlloc 分配的。
综上,当AllocationBase == BaseAddress的时候,AllocationBase 就是模块在进程内存中的起始地址,即HMODULE。
至此,VirtualQuery应该是比较明了了。
通俗说VirtualQuery的功能就是:以页为单位,遍历进程虚拟空间。
一点理解,高手路过请指正!