MemoryMappedFile

MemoryMappedFile


Represents a simple file-to-memory mapping.


Implements:

IObject 

Public:

Types:

NameDescription
 MemoryMappedFileAccess Specifies access capabilities and restrictions for a memory-mapped file or view.  
 MemoryMappedFileRights Specifies access rights to a memory-mapped file that is not associated with a file on disk.  

Properties:

NameDescription
 SafeMemoryMappedFileHandle (get) Gets the file handle of a memory-mapped file.  

Methods:

NameDescription
 CreateViewAccessor Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset, size, and access restrictions.  
 CreateViewStream Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset, size, and access type.  
 Equals Returns a boolean indicating if the value and this object instance are the same instance.  
 GetHashCode Returns a pseudo-unique number identifying this instance.  
 ToString Returns a string representation of this object instance.  

Remarks

A memory-mapped file maps the contents of a file to an application’s logical address space. Memory-mapped files enable programmers to work with extremely large files because memory can be managed concurrently, and they allow complete, random access to a file without the need for seeking. Memory-mapped files can also be shared across multiple processes.

The CreateFromFile and CreateFromFileStream methods create a memory-mapped file from a specified path or a FileStream of an existing file on disk. Changes are automatically propagated to disk when the file is unmapped.

The CreateNew method creates a memory-mapped file that is not mapped to an existing file on disk; and are suitable for creating shared memory for interprocess communication (IPC).

A memory-mapped file is associated with a name.

You can create multiple views of the memory-mapped file, including views of parts of the file. You can map the same part of a file to more than one address to create concurrent memory. For two views to remain concurrent, they have to be created from the same memory-mapped file. Creating two file mappings of the same file with two views does not provide concurrency.

Examples

The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.

The example uses the User-Defined type MyColor and passes it to several MemoryMappedViewAccessor methods. In order for the code sample to work, the MyColor type needs to be exposed through a public class in an ActiveX dll or Exe.

Public Type MyColor
    Red     As Integer
    Green   As Integer
    Blue    As Integer
    Alpha   As Integer
End Type

Public Sub Brightener()
    Const Offset As Long = &H10000000   ' 256 megabytes
    Const Length As Long = &H20000000   ' 512 megabytes
    
    Dim Mmf As MemoryMappedFile
    Dim Accessor As MemoryMappedViewAccessor
    
    Set Mmf = MemoryMappedFile.CreateFromFile("c:\ExtremelyLargeImage.data", FileMode.OpenExisting, "ImgA")
    
    ' Create a random access view, from the 256th megabyte (the offset)
    ' to the 768th megabyte (the offset plus length).
    Set Accessor = Mmf.CreateViewAccessor(Offset, Length)
    
    Dim i As Long
    Dim Color As MyColor
    
    ' Make changes to the view
    Do While i < Length
        Accessor.Read i, Color
        Brighten Color, 10
        Accessor.WriteValue i, Color
        
        i = i + LenB(Color)
    Loop
End Sub

Private Sub Brighten(ByRef Color As MyColor, ByVal Value As Integer)
    Const Int16Max As Integer = 32737
    
    Color.Red = Min(Int16Max, CLng(Color.Red) + Value)
    Color.Green = Min(Int16Max, CLng(Color.Green) + Value)
    Color.Blue = Min(Int16Max, CLng(Color.Blue) + Value)
    Color.Alpha = Min(Int16Max, CLng(Color.Alpha) + Value)
End Sub

See Also

Project CorLib Overview

Class MemoryMappedFile Overview

MemoryMappedFileStatic