windows 文件内存映射

static int MapLogFile(void)
{
    hFile = CreateFile(".\\db.bin",
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
        NULL);
    if (INVALID_HANDLE_VALUE == hFile) {
        goto err;
    }

    hMap = CreateFileMapping(hFile,
        NULL,
        PAGE_READWRITE,
        0, MAP_LENGTH,
        NULL);
    if(0 == hMap) {
        goto err_map;
    }

    MemAddrBegin = (unsigned char *)MapViewOfFile(hMap,
        FILE_MAP_ALL_ACCESS,
        0, 0,
        0);
    if(0 == MemAddrBegin) {
        goto err_view;
    }

    return 0;

err_view:
    CloseHandle(hMap);
err_map:
    CloseHandle(hFile);
err:
    return -1;
}

static int UnMapLogFile(void)
{
    UnmapViewOfFile(MemAddrBegin);
    CloseHandle(hMap);
    CloseHandle(hFile);

    return 0;
}

posted on 2014-07-25 16:47  JonnyLulu  阅读(364)  评论(0编辑  收藏  举报

导航