Search code examples
winapimemory-mapped-files

MapViewOfFile chunked loading of file


I want to map file into memory with chunk size equal system granularity. First chunk read without error and all others fails with error 5 (ERROR_ACCESS_DENIED). I tried run program with administrator privileges.

My code:

#include <windows.h>
#include <stdio.h>

int main() {
        HANDLE hFile = CreateFile(      TEXT("db.txt"),
                                                                GENERIC_READ,
                                                                FILE_SHARE_READ,
                                                                NULL,
                                                                OPEN_EXISTING,
                                                                FILE_ATTRIBUTE_NORMAL,
                                                                NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
                printf("[ERROR] File opening error %d\n", GetLastError());
                return 1;
        }

        printf("[DONE] File opened successfully.\n");

        HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
        if (hMap == NULL) {
                printf("[ERROR] Create mapping error %d\n", GetLastError());
                return 2;              
        }

        printf("[DONE] Create mapping successfully.\n");

        LARGE_INTEGER file_size = { };
        if (!GetFileSizeEx(hFile, &file_size)) {
                printf("[ERROR] Getiing filesize error %d\n", GetLastError());
                return 3;
        }

        printf("[DONE] Getting file size.\n");

        SYSTEM_INFO info = { };
        GetSystemInfo(&info);
        printf("[DONE] Getting system memory granularity %d.\n", info.dwAllocationGranularity);

        DWORD offset = 0;
        int size = 0;
        do {
                char* ENTRY = (char*)MapViewOfFile(hMap, FILE_MAP_READ, HIWORD(offset), LOWORD(offset), info.dwAllocationGranularity);
                if (ENTRY == NULL) {
                        printf("[ERROR] Map entry error %d\n", GetLastError());
                } else {
                        printf("[DONE] MAPPING PART WITH OFFSET %d\n", offset);
                        //printf("%s\n", ENTRY);
                }

                if (offset +  info.dwAllocationGranularity < file_size.QuadPart) {
                        offset += info.dwAllocationGranularity;
                } else {
                        offset = file_size.QuadPart;
                }
                //offset += size;      
                UnmapViewOfFile(ENTRY);
        } while (offset < file_size.QuadPart);




        CloseHandle(hMap);                             
        CloseHandle(hFile);
        system("pause");
        return 0;      
}

How I fix it?


Solution

  • You're using HIWORD and LOWORD for the offset in the call to MapViewOfFile, but these only take a 32-bit value and split it into two 16-bit halves - what you want is a 64-bit value split into two 32-bit halves.

    Instead you need HIDWORD and LODWORD, which are defined in <intsafe.h>:

    #define LODWORD(_qw)    ((DWORD)(_qw))
    #define HIDWORD(_qw)    ((DWORD)(((_qw) >> 32) & 0xffffffff))
    

    Like so:

    char* ENTRY = (char*)MapViewOfFile(hMap, FILE_MAP_READ, HIDWORD(offset), LODWORD(offset), info.dwAllocationGranularity);
    

    You need this even though your offset variable is 32 bit (in which case, HIDWORD will just return 0 and the full value of offset is passed as the low-order DWORD).