Memory Mapping a File

You can access a file though a memory object that is directly mapped to the file. Windows CE reflects any change to the memory-mapped object back to the file. Memory mapping can be more complicated to set up than traditional memory access; however, memory mapping simplifies file access. Instead of using a system-maintained pointer to write to the file, you can write directly to memory.

    To set up and access a file by using memory mapping

  1. Call the CreateFileForMapping function to open or create the memory-mapped file.

    You can open any file, including files that are created by CreateFile, for memory mapping. Like CreateFile, you can also create a file with CreateFileForMapping. For Windows CE version 2.10 and earlier, you can use CreateFileForMapping for read-only permission.

  2. Use the CreateFileMapping function to create an object in memory and to tie the object to the file that is opened by CreateFileForMapping.
  3. Use the MapViewOfFile function to create a view of the memory-mapped object.
  4. Use the pointer that is returned by MapViewOfFile to gain direct access to the memory-mapped object.
  5. Call the UnmapViewOfFile function to unmap the object view.
  6. Call the CloseHandle function to close the memory object.
  7. Call CloseHandle to close the file.