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);
MapViewOfFilesucceeds with the opened handlehSharedMemOpenedwith permissionFILE_MAP_READ.MapViewOfFilesucceeds with the created handlehSharedMemCreatedwith permissionFILE_MAP_ALL_ACCESS.MapViewOfFilefails with the opened handlehSharedMemOpenedwith permissionFILE_MAP_ALL_ACCESS.
MapViewOfFile(),FILE_MAP_ALL_ACCESSis equivalent toFILE_MAP_WRITE. Are you SURE you are opening a mapping object that was created with write permissions? Are you callingCreateFileMapping()andOpenFileMapping()in separate processes? If not, you shouldn't be using them together at all, let alone giving the mapping object a name. - Remy LebeauPAGE_READWRITEis not a valid argument forOpenFileMapping(). You probably wantFILE_MAP_ALL_ACCESSinstead. - Jonathan Potter