Question

Been Googling, but can't find an answer to this. Have not used MemoryMappedFile before. Have just been reading about them with a view to implementing basic IPC. Before I invest time prototyping can anyone tell me whether the following logic will work?

I had in mind to use a non-persisted memory-mapped file, so that it is automatically cleaned up after use.

  1. Process A opens MemoryMappedFile and writes to it.
  2. Process A launches process B
  3. Process A quits
  4. Process B performs OpenExisting on MemoryMappedFile created by process A
  5. Process B reads data from MemoryMappedFile and does processing...

Process A & B will both know the name of the file.

Will the file created by Process A remain until process B has disposed of it?

What are the mechanics, should process A not Dispose the file and let Process B do that?

If this logic will not work then I was considering that Process A wait for Process B to complete, but this is not ideal.

Thanks for any shedding of light ;)

Was it helpful?

Solution

That will work, except process B needs to open a handle to the memory mapped file before process A closes its handle. The reason is that the memory mapped file is destroyed when the last handle is closed.

(I assume we're talking Windows here.)


Edit: I got interested in this and did a bit more reading…

Probably the most correct and robust way to do this would be for process B to inherit a handle to to the memory mapping. Some links:

I think this last link provides you with a model answer by one of the most knowledgeable Windows programmers in the world. Do read the comments below the article as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top