19 const SIZE_T page_info_size =
sizeof(MEMORY_BASIC_INFORMATION);
21 while (start_va <
mask) {
23 memset(&page_info, 0, page_info_size);
24 const SIZE_T out = VirtualQueryEx(processHandle, (LPCVOID)start_va, &page_info, page_info_size);
25 const bool is_read = (out == page_info_size) ?
true :
false;
26 const DWORD error = is_read ? ERROR_SUCCESS : GetLastError();
27 if (error == ERROR_INVALID_PARAMETER) {
30 std::cout <<
"Nothing more to read: " << std::hex << start_va << std::endl;
34 if (error == ERROR_ACCESS_DENIED) {
35 std::cerr <<
"[ERROR] Cannot query the memory region. " << std::hex << start_va <<
" Error: " << std::dec << error << std::endl;
40 std::cerr <<
"[WARNING] Cannot query the memory region. " << std::hex<< start_va <<
" Error: " << std::dec << error <<
", skipping the page..." << std::endl;
44 if ((page_info.State & MEM_FREE) || (page_info.State & MEM_COMMIT) == 0) {
45 if (page_info.RegionSize != 0) {
47 start_va += page_info.RegionSize;
51 if (page_info.RegionSize == 0) {
68 MEMORY_BASIC_INFORMATION page_info = { 0 };
69 ULONGLONG next_va = 0;
72 ULONGLONG base = (ULONGLONG)page_info.BaseAddress;
73 next_va = base + page_info.RegionSize;
74 mem_region_info curr_info((ULONGLONG)page_info.AllocationBase, base, page_info.RegionSize);
76 if (region_bases.find(curr_info) != region_bases.end()) {
80 region_bases.insert(curr_info);
82 return region_bases.size();
87 DWORD number_of_entries = 1;
88 DWORD buffer_size =
sizeof(PSAPI_WORKING_SET_INFORMATION) + (number_of_entries *
sizeof(PSAPI_WORKING_SET_BLOCK));
89 PSAPI_WORKING_SET_INFORMATION* buffer =
reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*
>(calloc(1, buffer_size));
93 DWORD res = QueryWorkingSet(processHandle, buffer, buffer_size);
94 if (res == FALSE && GetLastError() == ERROR_BAD_LENGTH) {
98 number_of_entries =
static_cast<DWORD>(buffer->NumberOfEntries);
99 free(buffer); buffer = NULL;
105 std::cout <<
"Number of entries: " << std::dec << number_of_entries << std::endl;
107 return number_of_entries;