开发者

Why MapViewOfFile doesn't fail?

开发者 https://www.devze.com 2023-02-20 14:22 出处:网络
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 e

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 = M开发者_如何学JAVAapViewOfFile(mm, FILE_MAP_ALL_ACCESS, 0, 0, 1024 * 1024);

    if (addr != NULL) {
        MessageBox(0, NULL, NULL, 0);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消