Why does MapViewOfFile not work with ERROR_ACCESS_DENIED?

I came across this situation using the WinAPI MapViewOfFile function. A search on the Internet did not reveal any obvious corrections, so I will talk about my problem and solution.

Consider the following snippet:

 const char *name = "Global\\Object_Name"; unsigned long size = get_object_size(); HANDLE handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, name); if (!handle || handle == INVALID_HANDLE_VALUE) exit(GetLastError()); bool created = GetLastError() == 0; void *block = MapViewOfFile( handle, FILE_MAP_ALL_ACCESS, 0, 0, size); if (block == NULL) exit(GetLastError()); 

In one specific case, CreateFileMapping successfully returned a handle. GetLastError returned ERROR_ALREADY_EXISTS , therefore created == false . Now calling MapViewOfFile , using the same size that I passed to CreateFileMapping , returns NULL and GetLastError returns ERROR_ACCESS_DENIED : ERROR_ACCESS_DENIED . The process was run as administrator.

The MSDN documentation does not mention any reason for this situation. So why does CreateFileMapping succeed, but MapViewOfFile does not work?

+6
source share
2 answers

I am sure that there are many reasons why ERROR_ACCESS_DENIED can occur when MapViewOfFile called. In my specific situation, this was due to the size argument.

The hint is that created == false . It shows that the object "Global\\Object_Name" has already been created. For some reason, the creator of the call initialized a smaller section. For what looks like supervision, a second call to CreateFileMapping will happily give you a handle to an existing object, even if you ask for a larger match.

The call to MapViewOfFile now fails because it requests a view that is larger than the actual section.

So, if you are in a similar situation when the second MapViewOfFile call fails, check the size you are trying to switch to.

Maybe the second project is compiled with a different alignment of the structure, as a result, the sizeof() operator defines different values, or some other function of determining the size does not behave as expected.

+7
source

After much suffering, I finally discovered what causes this error in my application, in case someone else struggles with the same, the problem is not with the MapViewOfFile method, but with CreateFileMapping, the file size createFileMapping should be the size of the file, not the size of the item to read, if you do not know the size, then it should be 0, this does not apply to MapViewOfFile, since the value transmitted as the size is the length of the block you want to read / write.

Your code will look like this:

 const char *name = "Global\\Object_Name"; unsigned long size = get_object_size(); HANDLE handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 0, name); if (!handle || handle == INVALID_HANDLE_VALUE) exit(GetLastError()); bool created = GetLastError() == 0; void *block = MapViewOfFile( handle, FILE_MAP_ALL_ACCESS, 0, 0, size); if (block == NULL) exit(GetLastError()); 

And just putting it here to document what I found, unfortunately, it's hard to find this error when you don't know what causes it. Hope this saves a couple of hours to someone else.

+6
source

All Articles