How to use Memory Mapped Files in vb.net
Account Home | Help | Blog | Contact us | Log Out


Welcome to Kbytes > Articles

How to use Memory Mapped Files in vb.net

Posted By: siteadmin on 16/11/2018 00:53:00

How to use Memory Mapped Files in vb.net

Here is a simple set of functions that will permit you to create, read and overwrite MMF in vb.net

Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Security.AccessControl
Imports System.Security.Principal
 
Public Class memoryMappedFileManager
 
    Shared Function createMMF(MMFfilename As String, serialisedAppDetails As String) As MemoryMappedFile
 
        Dim mSec = New MemoryMappedFileSecurity
        mSec.AddAccessRule(New AccessRule(Of MemoryMappedFileRights)(New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), MemoryMappedFileRights.FullControl, AccessControlType.Allow))
 
        Dim mmf As MemoryMappedFile = MemoryMappedFile.CreateNew(MMFfilename, 32768, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, mSec, HandleInheritability.Inheritable)
        Dim stream As MemoryMappedViewStream = mmf.CreateViewStream()
 
        Dim writer As StreamWriter = New StreamWriter(stream)
        writer.Write(serialisedAppDetails)
        writer.Flush()
 
        Return mmf
 
    End Function
 
    Shared Function overwriteMMF(MMFfilename As String, serialisedAppDetails As String) As MemoryMappedFile
 
        Dim mmf As MemoryMappedFile = MemoryMappedFile.OpenExisting(MMFfilename)
 
        Dim stream As MemoryMappedViewStream = mmf.CreateViewStream()
 
        'write out remaining space to 32k as vbNull
        Dim pad = New String(vbNullChar, 32768 - serialisedAppDetails.Length)
 
        Dim writer As StreamWriter = New StreamWriter(stream)
        writer.Write(serialisedAppDetails & pad)
        writer.Flush()
 
        ' Debug.Print(Text)
        Return mmf
    End Function
 
 
    Shared Function readMMF(MMFfilename As String) As String
 
        Dim mmf As MemoryMappedFile = MemoryMappedFile.OpenExisting(MMFfilename, MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable)
        Dim stream As MemoryMappedViewStream = mmf.CreateViewStream()
 
        'Dim binaryReader = New BinaryReader(stream)
        'Dim length = binaryReader.Read()  'gives 11, length of "hello there" string.
        'Dim serialisedAppDetails As String = binaryReader.ReadChars(length)
 
        Dim streamReader = New StreamReader(stream)
        Dim serialisedAppDetails = streamReader.ReadToEnd()
 
        Return Replace(serialisedAppDetails, vbNullChar, "")
 
 
    End Function
End Class
 
Notes:
 
To use this MemoryMappedFilemanager in your Form (or console) app declare a property that is held in memory as long as the application is running 
 
Public Class Form1
 
    Property mmf As MemoryMappedFile
 
..
  Sub some_activity()
 
 
  mmf = memoryMappedFileManager.createMMF("orchMMF", textToWrite)
 
  End Sub
  
 
End Class

You can then use the Read and Overwrite MMF methods.

Note the memory mapped file here is limited to 32768 bytes (32K).   If you need to allocate more, increase the parameter in the above function.


blog comments powered by Disqus

Kbytes Home | Privacy Policy | Contact us | Testing Area

© 2004 - 2024 1 Oak Hill Grove Surbiton Surrey KT6 6DS Phone: +44(020) 8123 1321