Reputation: 176
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?
Upvotes: 2
Views: 3157
Reputation: 1
If you want your In-Memory MMF to be accessed across the process add "Global\\"
prefix to its name. Also, make sure to prevent the MMF from getting disposed of by adding it to a collection. You can try something similar as below:
var mmFName = $"Global\\{Guid.NewGuid().ToString()}";
var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew(
mmFName,
allocationSize,
MemoryMappedFileAccess.ReadWrite,
MemoryMappedFileOptions.None,
securityDescriptor,
HandleInheritability.Inheritable);
Upvotes: 0
Reputation: 111950
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.
Upvotes: 3