ByXiaoXie

Gary

头像

@byxiaoxie1月前

09/9
18:11
Home

C++ 查询内存页属性

std::string QueryMemInfo(LPVOID address)
{
	MEMORY_BASIC_INFORMATION meminfo;
	VirtualQueryEx(GameHandle, address, &meminfo, sizeof(meminfo));  //查询内存页属性保存到[meminfo]

	PVOID base_address = meminfo.BaseAddress;
	PVOID alloc_base = meminfo.AllocationBase;
	DWORD alloc_protect = meminfo.AllocationProtect;
	DWORD region_size = meminfo.RegionSize;
	DWORD state = meminfo.State;
	DWORD protect = meminfo.Protect;
	DWORD type = meminfo.Type;

	std::string s_alloc_protect;
	if (alloc_protect & PAGE_NOACCESS)                // 0x0001
		s_alloc_protect = "NoAccess";
	if (alloc_protect & PAGE_READONLY)                // 0x0002
		s_alloc_protect = "Readonly";
	else if (alloc_protect & PAGE_READWRITE)          // 0x0004
		s_alloc_protect = "ReadWrite";
	else if (alloc_protect & PAGE_WRITECOPY)          // 0x0008
		s_alloc_protect = "WriteCopy";
	else if (alloc_protect & PAGE_EXECUTE)            // 0x0010
		s_alloc_protect = "Execute";
	else if (alloc_protect & PAGE_EXECUTE_READ)       // 0x0020
		s_alloc_protect = "Execute_Read";
	else if (alloc_protect & PAGE_EXECUTE_READWRITE)  // 0x0040
		s_alloc_protect = "Execute_ReadWrite";
	else if (alloc_protect & PAGE_EXECUTE_WRITECOPY)  // 0x0080
		s_alloc_protect = "Execute_WriteCopy";
	if (alloc_protect & PAGE_GUARD)                   // 0x0100
		s_alloc_protect += "+Guard";
	if (alloc_protect & PAGE_NOCACHE)                 // 0x0200
		s_alloc_protect += "+NoCache";

	std::string s_state;
	if (state == MEM_COMMIT)
		s_state = "Commit ";
	else if (state == MEM_FREE)
		s_state = "Free   ";
	else if (state == MEM_RESERVE)
		s_state = "Reserve";
	else
		s_state = "Damned ";

	std::string s_protect;
	if (protect & PAGE_NOACCESS)
		s_protect = "NoAccess";
	if (protect & PAGE_READONLY)
		s_protect = "Readonly";
	else if (protect & PAGE_READWRITE)
		s_protect = "ReadWrite";
	else if (protect & PAGE_WRITECOPY)
		s_protect = "WriteCopy";
	else if (protect & PAGE_EXECUTE)
		s_protect = "Execute";
	else if (protect & PAGE_EXECUTE_READ)
		s_protect = "Execute_Read";
	else if (protect & PAGE_EXECUTE_READWRITE)
		s_protect = "Execute_ReadWrite";
	else if (protect & PAGE_EXECUTE_WRITECOPY)
		s_protect = "Execute_WriteCopy";
	if (protect & PAGE_GUARD)
		s_protect += "+Guard";
	if (protect & PAGE_NOCACHE)
		s_protect += "+NoCache";

	std::string s_type;
	if (type == MEM_IMAGE)
		s_type = "Image  ";
	else if (type == MEM_MAPPED)
		s_type = "Free   ";
	else if (type == MEM_PRIVATE)
		s_type = "Private";
	else
		s_type = "-      ";

	char buf[128] = { '/0' };
	sprintf(buf, "%8X %8X %25s %7s %25s %7s %8X", base_address, alloc_base,s_alloc_protect.c_str(), s_state.c_str(), s_protect.c_str(), s_type.c_str(), region_size);

	return std::string(buf);
}

int main(){
	HANDLE GameHandle = 0x0; //游戏句柄
	LPVOID BaseAddress = NULL; //游戏模块
	QueryMemInfo(GetBaseAddress + OFFSET_Role);  //调用方法
}

不提供完整的C++代码,仅查询内存页属性代码

C++ 查询内存页属性

加载中……