6

I am following this tutorial here

I am having a hard time figuring out how to get a string "THIS IS A TEST MESSAGE" to store in the memory mapped file and then pull it out on the other side. The tutorial says to use byte array. Forgive me I am new to this and trying on my own first.

Thanks, Kevin

##Write to mapped file

using System;
using System.IO.MemoryMappedFiles;

class Program1
{
    static void Main()
    {
        // create a memory-mapped file of length 1000 bytes and give it a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
        // write an integer value of 42 to this file at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        accessor.Write(500, 42);
        Console.WriteLine("Memory-mapped file created!");
        Console.ReadLine(); // pause till enter key is pressed
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}   


##read from mapped file  
using System;
using System.IO.MemoryMappedFiles;
class Program2
{
    static void Main()
    {
        // open the memory-mapped with a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
        // read the integer value at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        int value = accessor.ReadInt32(500);
        // print it to the console
        Console.WriteLine("The answer is {0}", value);
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}
4
  • Q: Are you successfully able to write and read back your Int32 "42" at location 500? If so, just extend the same concept to byte arrays: dotnetperls.com/convert-string-byte-array
    – paulsm4
    May 29, 2012 at 20:55
  • I think I've got the write working with strings. Will test and repost code changes.
    – kevp
    May 29, 2012 at 20:56
  • What I am trying to do is have one application write to shared mem, close, and then read the message. This example seems to work when both are on at the same time.
    – kevp
    May 29, 2012 at 21:05
  • In "classic Win32", the shared memory object should persist until all references to the object are deleted (i.e. even after the first process is closed). AFAIK, the same applies to a .Net program running under Windows.
    – paulsm4
    May 29, 2012 at 21:26

3 Answers 3

14

You can consider writing the length of the string and then the byte[] form of your string.
For example if I would like to write "Hello" then I convert it into bytes:

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes("Hello");

then do following while writing into the memory mapped file.

MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(54, (ushort)Buffer.Length);
accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length);

While reading first go to position 54 and read 2 bytes holding length of your string. Then you can read an array of that length and convert it into string.

MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
ushort Size = accessor.ReadUInt16(54);
byte[] Buffer = new byte[Size];
accessor.ReadArray(54 + 2, Buffer, 0, Buffer.Length); 
MessageBox.Show(ASCIIEncoding.ASCII.GetString(Buffer));
2
  • You, sir, are a life-saver for those who don't have time to think about their problems in detail. Jan 10, 2019 at 18:00
  • while reading it should be MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
    – skoley
    Apr 30, 2019 at 10:56
1

I used this to write the characters of a string:

string contentString = "Hello";
char[] charsToWrite = contentString.ToCharArray();
accessor.WriteArray(0, charsToWrite, 0, charsToWrite.Length);

This wrote wide characters. Both C# and C++ programs were able to read the data as wide characters.

1

It Works fine with CreateOrOpen instead of CreateNew! With the same code

MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(54, (ushort)Buffer.Length);
accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length);

and

MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
ushort Size = accessor.ReadUInt16(54);
byte[] Buffer = new byte[Size];
accessor.ReadArray(54 + 2, Buffer, 0, Buffer.Length); 
MessageBox.Show(ASCIIEncoding.ASCII.GetString(Buffer));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.