Microsoft KB Archive/301108

= HOW TO: Use a Memory-Mapped File in Windows CE =

Article ID: 301108

Article Last Modified on 12/1/2003

-

APPLIES TO


 * Microsoft Windows 2000 Service Pack 1
 * Microsoft Windows 2000 Service Pack 2
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Service Pack 1
 * Microsoft Windows 2000 Service Pack 2

-



This article was previously published under Q301108





IN THIS TASK
SUMMARY
 * Requirements
 * Setting Up and Accessing a File Using Memory Mapping
 * Troubleshooting

REFERENCES



SUMMARY
Memory mapping allows access to a file though a memory object that is mapped directly to the file. Memory-mapped files provide a method for interprocess communication and virtual memory region allocation larger than the 32 MB slot size limit. This article demonstrates how to use a memory-mapped file in Microsoft Windows CE.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
 * A Microsoft Windows CE device.
 * Microsoft Windows 2000 or Windows NT(r) 4.0 Workstation with SP5 or later.
 * Microsoft eMbedded Visual C++ version 3.0 (formerly known as the Microsoft Windows CE Toolkit for Visual C++ version 6.0).

back to the top

Setting Up and Accessing a File Using Memory Mapping

 * 1) To open or create a memory-mapped file, call the CreateFileForMapping function. If a single file is to be shared between applications, only one application should call CreateFileForMapping. All other applications should proceed to step 2.

You can open any file, including files created by the CreateFile function, for memory mapping. For Windows CE 2.10 and earlier, you can use CreateFileForMapping for read-only permission.
 * 1) Use the CreateFileMapping function to create an object in memory and tie it to the file that is opened by the CreateFileForMapping function.
 * 2) Use the MapViewOfFile function to create a view of the memory-mapped object.
 * 3) To gain direct access to the memory-mapped object, use the pointer that is returned by the MapViewOfFile function.
 * 4) Call the UnmapViewOfFile function to unmap the object view.
 * 5) Call the CloseHandle function to close the memory object.
 * 6) Call the CloseHandle function to close the file.

back to the top

Troubleshooting
Although 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.

back to the top

