MemoryMappedFileStatic

MemoryMappedFileStatic


Represents a simple file-to-memory mapping.


Public:

Methods:

NameDescription
 CreateFromFile Creates a memory-mapped file that has the specified access mode, name, capacity, and access type from a file on disk.  
 CreateFromFileStream Creates a memory-mapped file that has the specified name, capacity, access type, and disposal requirement from a file on disk.  
 CreateNew Creates a memory-mapped file that has the specified capacity and access type in system memory.  
 CreateOrOpen Creates or opens a memory-mapped file that has the specified capacity and access type in system memory.  
 OpenExisting Opens an existing memory-mapped file that has the specified name in system memory.  

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 MemoryMappedFileStatic Overview

MemoryMappedFile