DLL获取自己的模块句柄的方法
2008-12-06 14:20
411 查看
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);
}
http://blog.csdn.net/jackzhhuang/archive/2008/11/01/3201249.aspx 这几天看了一下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的功能就是:以页为单位,遍历进程虚拟空间。
一点理解,高手路过请指正!
相关文章推荐
- 获取DLL的模块的句柄的简单方法
- 获取句柄的方法总结(尤其是对于dll而言)
- WinAPI: GetModuleHandle - 获取一个模块(exe 或 dll)的句柄
- WinAPI: GetModuleHandle - 获取一个模块(exe 或 dll)的句柄
- 获取句柄的方法总结(尤其是对于dll而言)
- 获取句柄的方法总结(尤其是对于dll而言)
- MFC规则dll中获取当前模块句柄
- Win32 API 获取窗口句柄方法 分享
- Java的动态性支持学习六 - 方法句柄 获取和调用
- .net OCX 无法获取“****”控件的窗口句柄,不支持无窗口的ActiveX控 新解决方法
- win7 注册 卸载 报错: 模块 XXX 已加载 但对DllUnregisterServer 的调用失败 处理方法
- 安全狗导致IIS Worker Process 遇到了一个问题和错误模块 ntdll.dll的解决方法
- python学习四:import模块方法、可变参数、字典key判断、版本信息获取、列表解析、
- C#实现获取鼠标句柄的方法
- Apache限速模块mod_bw.dll及防盗链设置方法
- html5获取自己定位的方法
- c#获取dll物理路径的方法
- Python获取DLL和EXE文件版本号的方法
- Python获取DLL和EXE文件版本号的方法
- Delphi获取其它进程窗口句柄的3种方法