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);
MapViewOfFile
succeeds with the opened handlehSharedMemOpened
with permissionFILE_MAP_READ
.MapViewOfFile
succeeds with the created handlehSharedMemCreated
with permissionFILE_MAP_ALL_ACCESS
.MapViewOfFile
fails with the opened handlehSharedMemOpened
with permissionFILE_MAP_ALL_ACCESS
.
MapViewOfFile()
,FILE_MAP_ALL_ACCESS
is 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_READWRITE
is not a valid argument forOpenFileMapping()
. You probably wantFILE_MAP_ALL_ACCESS
instead. – Jonathan Potter