Reputation: 505
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 handle hSharedMemOpened
with permission FILE_MAP_READ
.MapViewOfFile
succeeds with the created handle hSharedMemCreated
with permission FILE_MAP_ALL_ACCESS
.MapViewOfFile
fails with the opened handle hSharedMemOpened
with permission FILE_MAP_ALL_ACCESS
.Upvotes: 0
Views: 1066
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