#include <iostream>
#include <windows.h>
#include <vector>
void outputMemInfos(const std::vector<MEMORY_BASIC_INFORMATION>& memInfos) {
printf("------------------------------------------------------------------------ \n");
printf("start \t end \t size \t state \t type \t protect \n");
printf("------------------------------------------------------------------------ \n");
for (const MEMORY_BASIC_INFORMATION& mbi: memInfos) {
printf("0x%012llX \t 0x%012X \t %8llx \t ", mbi.BaseAddress, ((uintptr_t)mbi.BaseAddress + (uintptr_t)mbi.RegionSize), mbi.RegionSize);
switch (mbi.State) {
case MEM_FREE: printf("MEM_FREE \t"); break;
case MEM_RESERVE: printf("MEM_RESERVE \t"); break;
case MEM_COMMIT: printf("MEM_COMMIT \t"); break;
default: printf("UNKNOW \t"); break;
}
switch (mbi.Type) {
case MEM_PRIVATE: printf("MEM_PRIVATE \t"); break;
case MEM_MAPPED: printf("MEM_MAPPED \t"); break;
case MEM_IMAGE: printf("MEM_IMAGE \t"); break;
default: printf("UNKNOW \t"); break;
}
if (mbi.Protect == 0) {
printf("---");
}
else if (mbi.Protect & PAGE_EXECUTE) {
printf("E--");
}
else if (mbi.Protect & PAGE_EXECUTE_READ) {
printf("ER-");
}
else if (mbi.Protect & PAGE_EXECUTE_READWRITE) {
printf("ERW");
}
else if (mbi.Protect & PAGE_READONLY) {
printf("-R-");
}
else if (mbi.Protect & PAGE_READWRITE) {
printf("-RW");
}
else if (mbi.Protect & PAGE_WRITECOPY) {
printf("WCOPY");
}
else if (mbi.Protect & PAGE_EXECUTE_WRITECOPY) {
printf("EWCOPY");
}
printf("\n");
}
}
bool ScanProcessMemory(HANDLE hProc, std::vector<MEMORY_BASIC_INFORMATION>& memInfos) {
SIZE_T stSize = 0;
PBYTE pAddress = (PBYTE)0;
SYSTEM_INFO systemInfo;
MEMORY_BASIC_INFORMATION mbi;
ZeroMemory(&systemInfo, sizeof(SYSTEM_INFO));
GetSystemInfo(&systemInfo);
pAddress = (PBYTE)systemInfo.lpMinimumApplicationAddress;
while (pAddress < (PBYTE)systemInfo.lpMaximumApplicationAddress) {
ZeroMemory(&mbi, sizeof(MEMORY_BASIC_INFORMATION));
stSize = VirtualQueryEx(hProc, pAddress, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
if (stSize == 0) {
pAddress += systemInfo.dwPageSize;
continue;
}
memInfos.push_back(mbi);
pAddress = (PBYTE)mbi.BaseAddress + mbi.RegionSize;
}
return true;
}
int main(int argc, char* argv[]) {
DWORD pid = 47968;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
std::vector<MEMORY_BASIC_INFORMATION> memInfos;
ScanProcessMemory(hProc, memInfos);
CloseHandle(hProc);
outputMemInfos(memInfos);
return 0;
}