Axel Williot
Axel Williot

Reputation: 505

Shared memory: MapViewOfFile returns error 5

I need a mechanism to share memory between some threads (usually in the same process, but sometimes not).

This code, which seems very basic, fails with error 5 (access denied) on MapViewOfFile:

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);

Upvotes: 0

Views: 1066

Answers (1)

Axel Williot
Axel Williot

Reputation: 505

The answer is in the comments:

PAGE_READWRITE is not a valid argument for OpenFileMapping(). You probably want FILE_MAP_ALL_ACCESS instead.

Upvotes: 1

Related Questions