I am trying to use VirtualAlloc2
with MemExtendedParameterAddressRequirements
to allocate memory to specific regions of address space. This is my first time using this new API but I haven't been able to ever get it to succeed. It always returns nullptr
and GetLastError
is ERROR_INVALID_PARAMETER
.
Here is a minimal example, which is almost identical to the MSDN example
#include <Windows.h>
void*
AllocateAlignedBelow2GB(size_t size, size_t alignment)
{
MEM_ADDRESS_REQUIREMENTS addressReqs = { 0 };
MEM_EXTENDED_PARAMETER param = { 0 };
addressReqs.Alignment = alignment;
addressReqs.HighestEndingAddress = (PVOID)(ULONG_PTR)0x7fffffff;
param.Type = MemExtendedParameterAddressRequirements;
param.Pointer = &addressReqs;
auto pVirtualAlloc2 =
(decltype(&::VirtualAlloc2))GetProcAddress(GetModuleHandle(L"kernelbase"),
"VirtualAlloc2");
return pVirtualAlloc2(
nullptr, nullptr,
size,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE,
¶m, 1);
}
int main()
{
void* p = AllocateAlignedBelow2GB(1024, 1024);
// p is now nullptr
// GetLastError() is ERROR_INVALID_PARAMETER
// Same behavior on 32-bit or 64-bit
return 0;
}
Just trying to even get this call to succeed at all, I have tried a number of things:
- Different allocation sizes
- Different alignments (from 1 to the system allocation granularity)
- Using a real process handle with
PROCESS_ALL_ACCESS
- Specifying different address bounds in
addressReqs
Windows SDK version 10.0.17763.0, KERNELBASE.dll is version 10.0.17134.441