title: MapViewOfFile returns Error 5 (ERROR_ACCESS_DENIED)
body: <p>I'm trying to develop a system that uses shared memory for communication between two processes. One process is a 32 bit application and uses a 32bit dll for the communication. The other process is a 64 bit application and uses a 64 bit dll which contains the exact same code for creating/opening the shared memory. I made it so whenever one process opens a handle to the memory and cannot open it, it automatically tries to create the memory. The other process will then try the same so whichever process runs the code first will create the memory while the other process will open a handle to the already existing memory.</p>
  
  <p>The code works absolutely fine when the shared memory is created by the 64 bit process/dll but whenever the 32 bit dll creates it, I get error 5 (ERROR_ACCESS_DENIED) returned when calling MapViewOfFile.</p>
  
  <p>I already checked if the size that I pass to any of the functions is different, for example because one of the structures has a different size depending on if it was compiled for 32 bit or 64 bit. However, this is not the case, the size is always the same in both processes.</p>
  
  <p>I also tried the code that was suggested in <a href="https://stackoverflow.com/a/5689496/5407251">this</a> answer without success.
  Does anyone have an idea why the code sometimes fails with error 5?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>static LPVOID lpMappedInputData = nullptr,
      lpMappedOutputData = nullptr;
  
  static HANDLE hInputFileMapping = NULL,
      hOutputFileMapping = NULL;
  
  HANDLE createOrOpenFileMapping(DWORD size, LPCSTR lpName)
  {
      HANDLE hMapFile = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, lpName);
  
      if (!hMapFile)
          DEBUG_LOG("OpenFileMapping failed! Error code: %i - Attempting to create the file mapping instead...\n", GetLastError());
  
      if (!hMapFile)
      {
          hMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, lpName);
  
          if (!hMapFile)
              DEBUG_LOG("CreateFileMapping failed! Error code: %i\n", GetLastError());
      }
  
      return hMapFile;
  }
  
  LPVOID mapViewOfFile(HANDLE hFileMappingObject, DWORD size)
  {
      LPVOID lpMappedData = MapViewOfFile(hFileMappingObject, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size);
  
      if (!lpMappedData)
      {
          CloseHandle(hFileMappingObject);
          DEBUG_LOG("MapViewOfFile failed! Error code: %i\n", GetLastError());
      }
  
      return lpMappedData;
  }
  
  bool Initialize()
  {
      DEBUG_LOG("Initializing the input file mapping...\n");
  
      hInputFileMapping = createOrOpenFileMapping(sizeof(InputData), "Local\\InputData");
  
      if (!hInputFileMapping)
          return false;
  
      lpMappedInputData = mapViewOfFile(hInputFileMapping, sizeof(InputData));
  
      if (!lpMappedInputData)
          return false;
  
      DEBUG_LOG("Initializing the output file mapping...\n");
  
      hOutputFileMapping = createOrOpenFileMapping(sizeof(OutputData), "Local\\OutputData");
  
      if (!hOutputFileMapping)
          return false;
  
      lpMappedOutputData = mapViewOfFile(hOutputFileMapping, sizeof(OutputData));
  
      if (!lpMappedOutputData)
          return false;
  
      return true;
  }
  
  void Deinitialize()
  {
      DEBUG_LOG("Deinitializing the file mappings...\n");
  
      if (lpMappedInputData)
          UnmapViewOfFile(lpMappedInputData);
  
      if (hInputFileMapping)
          CloseHandle(hInputFileMapping);
  
      if (lpMappedOutputData)
          UnmapViewOfFile(lpMappedOutputData);
  
      if (hOutputFileMapping)
          CloseHandle(hOutputFileMapping);
  
      DEBUG_LOG("Successfully deinitialized the file mappings!\n");
  }
  </code></pre>