FinalizeSections will fail if the allocated memory spans over the address 0x100000000 (4GB) since the addresses in the IMAGE_SECTION_HEADER are only 32-bit values. I could fix it by putting the following code after the allocation of the variable "code": #ifdef _WIN64 // check that memory-block does not span over the 4GB border if(code < (LPVOID)0x100000000 && code + alignedImageSize >= (LPVOID)0x100000000) { auto old_code = code; code = (unsigned char *)allocMemory(NULL, alignedImageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE, userdata); freeMemory(old_code, 0, MEM_RELEASE, userdata); if(code == NULL) { SetLastError(ERROR_OUTOFMEMORY); return NULL; } } #endif