My code. I have put the functions at the end.
long base = reserve(0x660000, 0x4000); // long reserve(long address, long size)
commit(base + 0x0000, 0x10); // long commit(long address, long size)
commit(base + 0x2000, 0x10);
printQueryInfo(base); //uses QueryVirtualMemoryInformation
reserve(0x66A000, 0x1000); //VirtualAlloc returns NULL
Console
Trying to allocate region of length 16384 bytes (decimal) at 0x660000.
Returned base address is: 0x660000
Trying to commit region of length 16 bytes (decimal) at 0x660000.
Returned base address is: 0x660000
Trying to commit region of length 16 bytes (decimal) at 0x662000.
Returned base address is: 0x662000
QueryVirtualMemoryInformation at 0x660000
AllocationBase: 0x660000
AllocationProtect: 0x4
Flags: 0x1
RegionSize: 0x4000
CommitSize: 0x2000
Trying to allocate region of length 4096 bytes (decimal) at 0x66A000.
Region allocation failed (base == NULL).
Returned base address is: 0x0
This is how I understand what allocating and committing means:
Is the rest of these 64kb now unusable, as only the first 5kb are allocated and you can not allocate other parts of that block?
How the functions are implemented in detail. I know that this is not pretty.
long reserve(long address, long size) {
printf("Trying to allocate region of length %li bytes (decimal) at 0x%lX.\n", size, address);
LPVOID base1 = VirtualAlloc(address, size, MEM_RESERVE, PAGE_READWRITE);
if (base1 == NULL) printf("Region allocation failed (base == NULL).\n");
long baseLong = base1;
printf("Returned base address is: 0x%lX\n", baseLong);
return baseLong;
}
long commit(long address, long size) {
printf("Trying to commit region of length %li bytes (decimal) at 0x%lX.\n", size, address);
LPVOID base1 = VirtualAlloc(address, size, MEM_COMMIT, PAGE_READWRITE);
if (base1 == NULL) printf("Region commit failed (base == NULL).\n");
long baseLong = base1;
printf("Returned base address is: 0x%lX\n", baseLong);
return baseLong;
}
void printQueryInfo(long address) {
WIN32_MEMORY_REGION_INFORMATION regionInformation[2]; //for some extra space... I get an error if it is only sizeof(WIN32_MEMORY_REGION_INFORMATION).
long l;
long informationBufferSize;
BOOL b = QueryVirtualMemoryInformation(
GetCurrentProcess(),
address,
MemoryRegionInfo,
®ionInformation,
28,
&informationBufferSize
);
//printf("informationBufferSize: %li\n", informationBufferSize); // prints 28
if (b == FALSE) {
DWORD lastError = GetLastError();
printf("QueryVirtualMemoryInformation returned false, code %i\n", (int)lastError);
}
else {
printf("QueryVirtualMemoryInformation at 0x%lX\n", address);
printf(" AllocationBase: 0x%lX\n", (long)(*regionInformation).AllocationBase);
printf(" AllocationProtect: 0x%lX\n", (long)(*regionInformation).AllocationProtect);
printf(" Flags: 0x%lX\n", (long)(*regionInformation).Flags);
printf(" RegionSize: 0x%zX\n", (size_t)(*regionInformation).RegionSize);
printf(" CommitSize: 0x%zX\n", (size_t)(*regionInformation).CommitSize);
}
}