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.
source share