初识内存,打印进程内存分布
2018-04-23 本文已影响21人
Fa1se003
#include "stdafx.h"
#include <WINDOWS.H>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
printf("处理器个数: %d\n",systemInfo.dwNumberOfProcessors);
printf("处理器分页大小: %d\n",systemInfo.dwPageSize);
printf("最小寻址单元: %.8X\n",systemInfo.lpMinimumApplicationAddress);
printf("最大寻址单元: %.8X\n",systemInfo.lpMaximumApplicationAddress);
DWORD dwPid = 1992;//进程id
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,0,dwPid);
MEMORY_BASIC_INFORMATION stuMemoryInfo;
LPCVOID addr = systemInfo.lpMinimumApplicationAddress;
vector<MEMORY_BASIC_INFORMATION> list;
while(addr < systemInfo.lpMaximumApplicationAddress)
{
VirtualQueryEx(hProcess,addr,&stuMemoryInfo,sizeof(stuMemoryInfo));
list.push_back(stuMemoryInfo);
addr = (LPCVOID) ((DWORD)addr+stuMemoryInfo.RegionSize);
}
//TODO循环填充到list控件中
for (int i=0;i<list.size();i++)
{
char stateBuff[20] = {0};
if(list[i].State == MEM_COMMIT)
{
wsprintf(stateBuff,"COMMIT");
}
else if(list[i].State == MEM_FREE)
{
wsprintf(stateBuff,"MEM_FREE");
}
else if(list[i].State == MEM_RESERVE)
{
wsprintf(stateBuff,"MEM_RESERVE");
}
else
{
wsprintf(stateBuff,"------");
}
printf("%.8X\t%.8X\t%s \n",list[i].BaseAddress,list[i].AllocationBase,stateBuff);
}
getchar();
CloseHandle(hProcess);
return 0;
}
/*
MEMORY_BASIC_INFORMATION结构体属性
PVOID BaseAddress; 块基地址 粒度4KB
PVOID AllocationBase; 区域基地址 粒度64KB
DWORD AllocationProtect; 区域被初次保留时赋予的保护属性 读 写 执行
SIZE_T RegionSize; 从BaseAddress开始,具有相同属性的页面的大小 按照4KB对齐
DWORD State; 块属性 MEM_FREE MEM_RESERVE MEM_COMMIT
DWORD Protect; 页面的属性,其可能的取值与AllocationProtect相同
DWORD Type; 内存区域类型 私有MEM_PRIVATE 映像MEM_IMAGE已映射MEM_MAPPED
*/
image.png