Windows获取模块基地址

获取模块基地址的几种方法
博客介绍了获取模块基地址的几种方法,包括直接将模块句柄转换为基地址、根据模块中地址得到模块句柄作为基地址、用 GetModuleHandleEx 根据地址获取所属模块基地址、使用 VirtualQueryEx 函数以及根据进程和模块句柄获取基地址等,还提到 SymGetModuleInfoW64 运行失败。

 

获取模块的基地址有如下几种方法:

1. 模块句柄就是模块基地址(或称模块加载地址),直接将模块句柄转换为模块基地址

2. 根据模块中的地址得到模块句柄,模块句柄就是基地址
GetModuleHandleEx

3. 根据模块中的地址得到这个地址所属模块的基地址
但是 SymGetModuleInfoW64 总是运行失败

4. 使用 VirtualQueryEx 函数

5. 根据进程句柄和模块句柄得到模块的基地址
GetModuleInformation

代码如下

#include "stdafx.h"
#include <Windows.h>
#include <Psapi.h>
#include <dbghelp.h>
#include <string>
#pragma comment(lib, "Psapi.lib")
#pragma comment(lib, "Dbghelp.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    // 1.
    printf("模块句柄就是模块基地址 : 0x%X\n", ::GetModuleHandle(L"kernel32.dll"));

    // 2.
    HMODULE hMoudle = nullptr;
    ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)&GetCurrentProcess, &hMoudle);
    printf("GetModuleHandleEx : 0x%X\n", hMoudle);
    
    // 3.
    IMAGEHLP_MODULEW64 im = {0};
    im.SizeOfStruct = sizeof(im);
    if (::SymGetModuleInfoW64(::GetCurrentProcess(), (DWORD64)&GetCurrentProcess, &im))
    {
        printf("SymGetModuleInfoW64 : 0x%X\n", im.BaseOfImage);
    }
    else
    {
        printf("SymGetModuleInfoW64 failed, LastError : %d\n", ::GetLastError());
    }

    // 4.
    MEMORY_BASIC_INFORMATION mbi;
    if (::VirtualQueryEx(::GetCurrentProcess(), (LPCVOID)&GetCurrentProcess, &mbi, sizeof(mbi)) != 0)
    {
        printf("VirtualQueryEx : 0x%X\n", mbi.AllocationBase);
    }
    else
    {
        printf("VirtualQueryEx failed, LastError : %d\n", ::GetLastError());
    }

    // 5.
    MODULEINFO moduleInfo;
    ::GetModuleInformation(::GetCurrentProcess(), ::GetModuleHandle(L"kernel32.dll"), &moduleInfo, sizeof(moduleInfo));
    printf("GetModuleInformation:0x%X\n", moduleInfo.lpBaseOfDll);

    getchar();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值