Search code examples
c#memory-mapped-files

MemoryMappedFile.openExisting : specified file not found when using 2 different process


I am using 2 different processes, Say -

Process1.exe : perform some operation and updates a string variable which will be saved in a MemoryMappedFile(To acheive IPC) which is file

Process2.exe: Calls Process1. After completion of "Process1", it tries to open MemoryMappedFile file and gets the string for further use.

here are the code snippets -

Process1.exe

//Some code
public void DoSomeStuff()
{
onst int MMF_MAX_SIZE = 4096;
const int MMF_VIEW_SIZE = 4096;
try
   {
     using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("file", MMF_MAX_SIZE, MemoryMappedFileAccess.ReadWrite))
     {
          using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, MMF_VIEW_SIZE))
          {
               Message message1;
               message1.strName = "Some name";
               BinaryFormatter formatter = new BinaryFormatter();
               formatter.Serialize(stream, message1);
          }
     }
   }
catch(Exception e)
{
     MessageBox.Show(e.Message);
}
}

Process2.exe

        const int MMF_VIEW_SIZE = 4096;

        using (MemoryMappedFile file = MemoryMappedFile.OpenExisting("file"))
        {
            using (MemoryMappedViewStream stream = file.CreateViewStream(0, MMF_VIEW_SIZE))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                byte[] buffer = new byte[MMF_VIEW_SIZE];
                Message message1;
                if (stream.CanRead)
                {
                    stream.Read(buffer, 0, MMF_VIEW_SIZE);
                    using (MemoryStream ms = new MemoryStream(buffer))
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                        message1 = (Message)formatter.Deserialize(ms);
                        string name = message1.strName;
                    }
                }
            }
        }

Here is my Message class which is written inside both the processes,

class Message
{
    public string strName;
}

Issue: after completion of Process1 it has successfully written the string data to MemoryMappedFile but when I am trying to open file in Process2 using line

MemoryMappedFile file = MemoryMappedFile.OpenExisting("file")

I am getting error - Specified file not found.

I am very new with MemoryMappedFile implementation to acheive IPC. Can someone suggest what's wrong I'm doing here?


Solution

  • The MemoryMappedFile.CreateOrOpen() you used will create a non-persisting memory mapped file. At the end of the using (MemoryMappedFile mmf =) (or at the end of Process1) the file will be "destroyed" (it will cease to exist).

    Solution: use a real file.

    Other solution could be to open the memory map in the parent process before calling the child process. There is even a CreateOrOpen overload that accepts a HandleInheritability to pass the handle to the child process (I don't know exactly how), but perhaps it isn't necessary.