Maybe you're misunderstanding the role of dwLength.
dwLength is the size of the buffer for MEMORY_BASIC_INFORMATION.
So you shouldn't let it greater than sizeof(MEMORY_BASIC_INFORMATION).
You'd better do like this.
MEMORY_BASIC_INFORMATION meminfo;
SIZE_T size = VirtualQuery(pMem2, &meminfo, sizeof(MEMORY_BASIC_INFORMATION));