This is a mirror of official site: http://jasper-net.blogspot.com/

Programming Memory-Mapped Files with .Net Framework

| Tuesday, December 21, 2010
Introduction

First of all, what is memory-mapped file or MMF? MMF is a kernel object that maps a disk file to a region of memory address space as the committed physical storage. In plain English, MMF allows you to reserve a range of addresses and use a disk file as the physical storage for the reserved addresses. When a MMF is created, you access the mapped disk file as if you were accessing memory. MMF makes accessing files easy. For example, Windows loads EXEs or DLLs using MMF. MMF is a corner stone for almost all modern forms of inter-process communication (or IPC). As a system developer, I felt inconvenient when early .Net Framework releases did not support MMF. The good news is that starting version 4, .Net Framework has built-in support for MMF!

This article has two objectives:

The tutorial will be focused on sample MMF applications. For theoretical discussions, Refer to Jeffery Richter's classic text "Programming Applications for Microsoft Windows - 4th Ed.", Chapter 17. For .Net Framework 4 MMF specifications, check out this MSDN page.
The tutorial is written for programmers who are familiar with both C# and native (unmanaged) C/C++ languages. In order to build and run these samples, you should have Visual Studio 2010 installed on your machine.
Imagine you have got some modules or applications written in native C/C++ and you want to use them. The old modules use MMF to exchange data. Without MMF support in earlier .Net, you can do two things among others:

Platform Invoke Win32 MMF support. You need to handle lots of marshalling between managed and unmanaged worlds.
You can always use Windows debug symbol support, which allows to access variables as symbols. However, this mechanism is rather involving and beyond beginners comfort level. You have to fiddle with marshalling as well.
With .Net Framework 4, you just write an MMF-enabled application to exchange data with old C/C++ applications directly. This article will demonstrate how to write MMF-enabled .Net applications. We start from basic examples. Then we'll move on to more advanced use of MMF in the shared memory design.

Simple MMF Applications

We demonstrate three sample MMF applications:

File Copy
Simple IPC
Single Instance

Read more: Codeproject

Posted via email from .NET Info

0 comments: