The 32-bit failure reproduced for me on Win 10 22H2.
Instead of checking the memory for a variable I suggest you use VirtualAlloc so that you can specific the memory size and the page protection values. That way you can determine if the information returned in the structure makes sense.
In my quick and dirty 32-bit test I found that adding the additional 8 bytes to the end of the structure made sense since it kept the size of pointers consistent at 4 bytes and the other memory attributes agreed with the call to VirtualAlloc for region size and protect values.
Update:
This is the code I used-
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
DWORD ShowLastError(LPCTSTR pszText, DWORD dwError = GetLastError());
typedef struct _MY_WIN32_MEMORY_REGION_INFORMATION : WIN32_MEMORY_REGION_INFORMATION {
#ifndef _WIN64
ULONG Extra1;
ULONG Extra2;
#endif
} MY_WIN32_MEMORY_REGION_INFORMATION;
int main()
{
SIZE_T ReturnSize;
MY_WIN32_MEMORY_REGION_INFORMATION mi;
PVOID pv = VirtualAlloc(NULL, 8192, MEM_RESERVE, PAGE_READWRITE);
_tprintf_s(_T("Memory Reserved by VirtualAlloc at 0x%p\n"), pv);
if (QueryVirtualMemoryInformation(GetCurrentProcess(), pv, MemoryRegionInfo, &mi, sizeof mi, &ReturnSize))
{
_tprintf_s(_T("Allocation BaseAddress is 0x%p\n"), mi.AllocationBase);
_tprintf_s(_T("Region Size is %Iu, Commit Size is %Iu\n"), mi.RegionSize, mi.CommitSize);
_tprintf_s(_T("Page Protect is %X\n"), mi.AllocationProtect);
}
else
ShowLastError(_T("QueryVirtualMemoryInformation"));
PBYTE pCommit = (PBYTE)pv + 4096;
PVOID pv2 = VirtualAlloc(pCommit, 4096, MEM_COMMIT, PAGE_READWRITE);
_tprintf_s(_T("\nMemory Committed by VirtualAlloc at 0x%p\n"), pCommit);
if (QueryVirtualMemoryInformation(GetCurrentProcess(), pv2, MemoryRegionInfo, &mi, sizeof mi, &ReturnSize))
{
_tprintf_s(_T("Allocation BaseAddress is 0x%p\n"), mi.AllocationBase);
_tprintf_s(_T("Region Size is %Iu, Commit Size is %Iu\n"), mi.RegionSize, mi.CommitSize);
_tprintf_s(_T("Page Protect is %X\n"), mi.AllocationProtect);
}
else
ShowLastError(_T("QueryVirtualMemoryInformation"));
PVOID pv3 = VirtualAlloc(pv, 4096, MEM_COMMIT, PAGE_READWRITE);
_tprintf_s(_T("\nMemory Committed by VirtualAlloc at 0x%p\n"), pv3);
if (QueryVirtualMemoryInformation(GetCurrentProcess(), pv3, MemoryRegionInfo, &mi, sizeof mi, &ReturnSize))
{
_tprintf_s(_T("Allocation BaseAddress is 0x%p\n"), mi.AllocationBase);
_tprintf_s(_T("Region Size is %Iu, Commit Size is %Iu\n"), mi.RegionSize, mi.CommitSize);
_tprintf_s(_T("Page Protect is %X\n"), mi.AllocationProtect);
}
else
ShowLastError(_T("QueryVirtualMemoryInformation"));
return 0;
}
DWORD ShowLastError(LPCTSTR pszText, DWORD dwError)
{
LPTSTR pszMsg = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, 0, (LPTSTR)&pszMsg, 0, NULL);
_tprintf_s(_T("%s : %s (%u)\n"), (pszText != NULL ? pszText : _T("Error was")), pszMsg, dwError);
LocalFree(pszMsg);
return dwError;
}