HOW TO: Use a Memory-Mapped File in Windows CE (301108)



The information in this article applies to:

  • Microsoft Windows 2000 Server SP1
  • Microsoft Windows 2000 Server SP2
  • Microsoft Windows 2000 Advanced Server SP1
  • Microsoft Windows 2000 Advanced Server SP2
  • Microsoft Windows 2000 Professional SP1
  • Microsoft Windows 2000 Professional SP2

This article was previously published under Q301108

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

REFERENCES

For more information, see the Microsoft Windows CE 3.0 Software Developer documentation at the following Microsoft Developer Network (MSDN) Web site: back to the top

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbhowto kbHOWTOmaster KB301108 kbAudDeveloper