CreateFileMapping and OpenFileMapping not cooperating in different processes

12,052

Solution 1

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.

Solution 2

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.

Share:
12,052
Max
Author by

Max

Updated on June 05, 2022

Comments

  • Max
    Max almost 2 years

    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();
    
  • Max
    Max over 11 years
    Typed enumerations have never been more desirable to me... Thanks for spotting the problem!