Skip to content

Instantly share code, notes, and snippets.

@jedwardsol
Last active July 7, 2022 15:34
Show Gist options
  • Save jedwardsol/9d4fe1fd806043a5767affbd200088ca to your computer and use it in GitHub Desktop.
Save jedwardsol/9d4fe1fd806043a5767affbd200088ca to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <limits>
std::ostream &operator<<(std::ostream &out,const MEMORY_BASIC_INFORMATION &region)
{
std::ostringstream string;
auto regionStart = reinterpret_cast<uintptr_t>(region.BaseAddress);
auto regionEnd = regionStart + region.RegionSize;
auto width = std::numeric_limits<uintptr_t>::digits/4;
string << std::setw(width) << std::hex << regionStart << "-" << std::setw(width) << std::hex << regionEnd ;
switch(region.State)
{
case MEM_COMMIT:
string << " Committed ";
break;
case MEM_FREE:
string << " Free ";
break;
case MEM_RESERVE:
string << " Reserved ";
break;
}
if(region.State == MEM_COMMIT)
{
auto mask = PAGE_GUARD | PAGE_NOCACHE | PAGE_WRITECOMBINE;
switch(region.Protect & ~mask)
{
case PAGE_EXECUTE:
case PAGE_EXECUTE_READ:
string << " Executable ";
break;
case PAGE_EXECUTE_READWRITE:
string << " Executable Writable ";
break;
case PAGE_EXECUTE_WRITECOPY:
string << " Executable CopyOnWrite ";
break;
case PAGE_NOACCESS:
string << " No Access ";
break;
case PAGE_READONLY:
string << " Readable ";
break;
case PAGE_READWRITE:
string << " Readable Writable";
break;
case PAGE_WRITECOPY:
string << " Readable CopyOnWrite";
break;
}
if(region.Protect & PAGE_GUARD)
{
string << " Guard";
}
}
out << string.str();
return out;
}
int main()
{
char *address{nullptr};
MEMORY_BASIC_INFORMATION region;
while(VirtualQuery(address,&region,sizeof(region)))
{
std::cout << region << "\n";
address += region.RegionSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment