2
votes

I'm trying to use CreateFileMapping and OpenFileMapping to share memory between processes. This isn't working as I want it to - OpenFileMapping returns null and GetLastError is 5 - access denied. Any ideas what I am doing wrong? Name is something like MemoryTest.

Edit:

using CreateFileMapping both times I can read the data written in the other process. The reason this is a problem is that I get Error 183 - memory area already exists. However, it still returns a handle to the existing memory.

var map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(int), name.c_str());

....

var handle = MapViewOfFile(map_handle, FILE_MAP_ALL_ACCESS , 0, 0, 0)

*handle = 10;

UnMapViewOfFile(map_handle);

getchar();

Other process:

var map_handle = OpenFileMapping(PAGE_READWRITE, false, name.c_str())

....

var handle = MapViewOfFile(map_handle, FILE_MAP_ALL_ACCESS , 0, 0, 0) //returns null

var out = *handle;

getchar();

This works for the second process though:

var map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(int), name.c_str());

....

var handle = MapViewOfFile(map_handle, FILE_MAP_ALL_ACCESS , 0, 0, 0) //returns null

var out = *handle;

getchar();
2
0x00000005 = ERROR_ACCESS_DENIED = "Access is denied." Can it be that UAC is enabled and one of the apps runs with elevated privileges OR the apps run under different accounts?Roman R.
OpenFileMapping does not accept PAGE_READWRITE, it takes FILE_MAP_*.Roman R.
@RomanR. They are both run in different cmd windows from my dev machine - UAC is on. Shouldn't it work anyway?Max
183 = 0x000000B7 = ERROR_ALREADY_EXISTS = "Cannot create a file when that file already exists.", which is specifically a name collision, not memory area. CreateFileMapping on MSDN explains what exactly this means.Roman R.
@RomanR. But it says there it returns the already existing object - also I can read the written data even though I get "ERROR ALREADY EXISTS". It just doesnt exactly right getting an error, even though it works...Max

2 Answers

8
votes

Simple things to be aware of from the very start:

  • Error code 5: ERROR_ACCESS_DENIED "Access is denied."
  • Error code 183: ERROR_ALREADY_EXISTS "Cannot create a file when that file already exists."

ERROR_ALREADY_EXISTS is a documented behavior and is an indication of scenario that you do receive handle, but it is a handle to already existing object, not created.

The problem with not working OpenFileMapping is around its first argument: the API function expects values/flags from another enumeration, it takes FILE_MAP_* values and not PAGE_*. Incorrect argument results in failure to open you the mapping you want.

2
votes

In case someone else needed, in my case the error has nothing to do with the access to the file, it's with the size provided to the CreateFileMapping, after spending hours with a similar error I'd to use a working sample posted somewhere else and line by line compare what was the difference.

If you don't know the size of the file when executing the CreateFileMapping you need to use 0, this will tell the API to use the file size of the mapped file. Most of the answers in SO around this are wrong and people is not bothering testing what is the problem about, I wasted hours reading other posts with similar suggestions.

To solve the problem the code should look like this:

var map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 0, name.c_str());

Hope this saves hours to other fellow developers.