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

这段代码看起来很基础,但在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

最佳答案

答案在评论中:

PAGE_READWRITE不是OpenFileMapping()的有效参数。您可能需要FILE_MAP_ALL_ACCESS代替。

关于c++ - 共享内存:MapViewOfFile返回错误5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52614485/

10-08 21:04