views:

785

answers:

5

Hi, I cant find any useful answer for this question, although it has been asked in a different way several times

I want to share a memory between 2 processes (2 different applications). so that one of them can write to that memory and the other can read.

is this possible in .NET? how?

Thanks

+1  A: 

Use a named pipe.

It's a mechanism that allows one process to write sequential data into a stream, from which the other process can read.

Note that named pipe only allow sequential read. If you need to use more elaborated queries, you might want to use client-server architecture. In this case, the reader process queries the other process for information using network sockets.

Adam Matan
I don't want to use named pipes, WCF, remoting ... etc ... that is why I specified "shared memory", shared memory between 2 Apps are easy to establish in c++, but I cant find a way to do it in C#.net
TB
I haven't seen any direct implementation of Shared memory, but you can easily implement a third process that would handle reads and writes of both processes using sockets.
Adam Matan
A: 

IPC

If you are talking about Inter Process Communication. There are several possibilities like using tcp/udp sockets, mailslots, named pipes, memory mapped files, windows messages, etc.

.Net also offers some higher level IPC like .Net remoting and WCF which use the previous mentioned techniques. You can read more about it here.

Memory Mapped Files

If you really want to share a block of memory instead of communication then maybe memory mapped files are what you want. .Net 4.0 has MemoryMappedFile for that. If you don't or can't use .Net 4.0 you could implement it yourself as in this win32 example. Or you could try this code or see if you can use MemoryMappedFileStream mentioned here and here. Or use this FileMap class.

Lars Truijens
I don't want to use named pipes, WCF, remoting ... etc ... that is why I specified "shared memory", shared memory between 2 Apps are easy to establish in c++, but I cant find a way to do it in C#.netI am using .NET 2.0, is there anyway to solve this using it? I dont want to go throw 3rd parties
TB
Updated my answer with more information
Lars Truijens
+4  A: 

Right now, .NET doesn't support sections (aka Memory Mapped Files). It will soon, the 4.0 version has the System.IO.MemoryMappedFiles namespace. There is a good reason it took so long and also the reason you are not going to be happy with this addition. Using pointers is mandatory in MMFs, the shared memory is made available at a specific address. To share a value, you'll have to write it to a specific address.

Pointers are however fundamentally incompatible with the managed memory model. Objects are created in a garbage collected heap, the collector moves them around as needed to keep the heap compacted. In a managed language, you have a reference to such an object, also knows as a "tracking handle". The C/C++ equivalent is a pointer to a pointer. The CLR does support the notion of "pinning", it converts a reference to a pointer. It implements this by marking the object as unmoveable. That however doesn't help implement shared memory through an MMF, the object is pinned in the GC heap.

To make an MMF work, the object needs to be copied from the GC heap to the shared memory. That requires serialization. The .NET 4.0 class is named MemoryMappedViewStream. You probably can see where this is going, this is indistinguishable from using a named pipe or a socket. Getting data in and out of an MMF takes the same amount of effort. An MMF is merely slightly more efficient because the underlying buffer is not in the kernel memory pool.

You can break the rulez and do so today. You can P/Invoke the CreateFileMapping, OpenFileMapping and MapViewOfFile you need to create an MMF. And use the unsafe keyword so you can create pointers. You'll need to use value types (like struct) for the shared memory elements or use the Marshal class.

Hans Passant
You are assuming that "sharing data" must mean sharing managed objects directly. What if someone wants to share some "plain" data?
devdimi
Not sure what "plain" means. But you still can't affect the address of value types directly, they live on the stack or the GC heap. A pointer is required to get them written to a MMF.
Hans Passant
A: 

As already mentioned use Memory Mapped Files. Implementation for .NET < 4 available at: http://github.com/tomasr/filemap/
I have used and it works flawless. You may have some security issues when sharing between elevated/not elevated processes - the solution is to initialize the memory mapped file in the elevated process and set its security attributes accordingly.

devdimi
A: 

The is an implementation , plus description at http://www.softwareinteractions.com/blog/2010/1/21/interprocess-communications-using-shared-memory.html

Laureano