Memory Mapping a File
Memory-mapping allows access to a file though a memory object that is mapped directly to the file. Memory-mapped files provide a method both for interprocess communication, and as a way to allocate virtual memory regions larger than the 32MB slot size limit.
While memory mapping is somewhat more complicated to set up than traditional memory access it simplifies file access by eliminating the need for a system-maintained pointer for read and write operations. Changes are written directly to memory and Windows CE reflects any change to the memory-mapped object back to the file.
To set up and access a file by using memory mapping
- Call the CreateFileForMapping function to open or create the memory-mapped file. If a single file is to be shared between applications, then only one application should call CreateFileForMapping. All other applications should skip to step 2.
You can open any file, including files that are created by the CreateFile function, for memory mapping. For Windows CE 2.10 and earlier, you can use CreateFileForMapping for read-only permission.
- Use the CreateFileMapping function to create an object in memory and to tie the object to the file that is opened by the CreateFileForMapping function.
- Use the MapViewOfFile function to create a view of the memory-mapped object.
- Use the pointer that is returned by the MapViewOfFile function to gain direct access to the memory-mapped object.
- Call the UnmapViewOfFile function to unmap the object view.
- Call the CloseHandle function to close the memory object.
- Call the CloseHandle function to close the file.