2
votes

MSDN says

If a file mapping object is backed by the paging file (CreateFileMapping is called with the hFile parameter set to INVALID_HANDLE_VALUE), the paging file must be large enough to hold the entire mapping. If it is not, MapViewOfFile fails.

But this code works even if the pagefile doesn't exist. Why?

HANDLE mm;
LPVOID addr;

mm = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 1024 * 1024, NULL);
if (mm != NULL) {
    addr = MapViewOfFile(mm, FILE_MAP_ALL_ACCESS, 0, 0, 1024 * 1024);

    if (addr != NULL) {
        MessageBox(0, NULL, NULL, 0);
    }
}
1
How did you delete the pagefile? - David Heffernan
The code looks like it should definitively not show a messagebox on a system that has no pagefile (just to be sure, you're saying it does show the message box?). Given SEC_COMMIT, the operating system has to make sure that memory actually exists (not only in theory as in SEC_RESERVE), I don't see how it could do that without a page file, other than by locking the entire memory into RAM as non-pageable, which it certainly won't do. - Damon
Clearly you're wrong and the system still has a pagefile to address to, otherwise the call would simply fail. - Assaf Levy
the pagefile doesn't exist. I've set "no page file" in system properties->perfomance->virtual memory. - a0987
and yes it does show the message box. - a0987

1 Answers

2
votes

Well, why would it fail? Pages allocated with VirtualAlloc() are mapped to the paging file as well. That doesn't fail, you couldn't get any real program started. There is otherwise no problem creating a MMF that isn't backed by the paging file, the memory cannot be unmapped anyway, it is permanently stuck in RAM.

Don't assume that the documented rules are still valid when you do something this unusual.