共享内存:MapViewOfFile返回错误5

时间:2018-10-02 18:42:10

标签: c++ winapi shared-memory

我需要一种在一些线程之间共享内存的机制(通常在同一进程中,但有时不是)。

此代码看起来很基础,但在MapViewOfFile上由于错误5(访问被拒绝)而失败:

HANDLE hSharedMemCreated = CreateFileMapping(
    INVALID_HANDLE_VALUE,    // use paging file
    NULL,                    // default security
    PAGE_READWRITE,          // read/write access
    0,                       // maximum object size (high-order DWORD)
    10000,                // maximum object size (low-order DWORD)
    "testFileMapping");                 // name of mapping object
HANDLE hSharedMemOpened = OpenFileMapping(
    PAGE_READWRITE,          // read/write access
    FALSE,
    "testFileMapping"
);
void* location = MapViewOfFile(
    hSharedMemOpened,   // handle to map object
    FILE_MAP_ALL_ACCESS, // read/write permission
    0,
    0,
    10);
  • MapViewOfFile成功打开,并获得权限hSharedMemOpened的句柄FILE_MAP_READ
  • MapViewOfFile成功创建并获得权限hSharedMemCreated的句柄FILE_MAP_ALL_ACCESS
  • MapViewOfFile失败,打开的句柄hSharedMemOpened的权限为FILE_MAP_ALL_ACCESS

1 个答案:

答案 0 :(得分:0)

答案在评论中:

/pathtoFile/window/LPA/cavin_l/LPA_formula_24rw39.csv /pathtoFile/window/LPA/cavin_l/LPA_formula_24rw39.csv 对于PAGE_READWRITE无效。您可能需要OpenFileMapping()