a0987
a0987

Reputation: 21

Why MapViewOfFile doesn't fail?

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);
    }
}

Upvotes: 2

Views: 685

Answers (1)

Hans Passant
Hans Passant

Reputation: 941675

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.

Upvotes: 2

Related Questions