IPC or Interprocess Communications in C#

Someone asked me about a sample application of IPC or Interprocess Communications using memory mapped files

What is IPC or Interprocess Communications you ask.

The easiest way to explain it is by saying it allows two application to talk to each other.

 
The following IPC mechanisms are supported by Windows:


Clipboard, COM, Data Copy, DDE, File Mapping, Mailslots, Pipes, RPC, Windows Sockets

Click the link below for much more details.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx#base.using_dde_for_ipc

In this post I'll be exploring Memory Mapped Files using .net C# to allow multiple applications to talk to each other and pass information.

 

In the distant past I have only used DDE to allow applications to talk directly to each other so this was pretty interesting to me.

In this example I will be using Memory Mapped Files. Mapped Files are fairly easy to implement in .Net

Memory Mapped Files are implemented by using the System.IO.MemoryMappedFiles Namespace

In the example below I have two very simple methods, CreateOrOpenMappedFile, and ReadMemoryMappedFille

Note below that we briefly use Mutex ( from system.threading.Mutex ) to capture and hold the thread that we are using for communications for our memory mapped file then we release it, ensuring that we are the only one who can change it at that time.

More on Mutex here http://msdn.microsoft.com/en-us/library/system.threading.mutex%28v=vs.110%29.aspx

I created one application and made a copy calling the applications app1.exe and app2.exe. Based on a command line switch I set one to be the sender and one as receiver ( actually they both act as sender and receiver. I just needed a way to specify different messages based on how the application was started ).

When using memory mapped files multiple applications can gain access to the same memory space to share messages and data, that they normally would not have access to. See the image below


 


The following code is really the heart of the application, the rest is just fluff. But the full application is supplied below for your enjoyment.


        //-----------------------------------------------------------------------------------
        //--Create or Open a memory mapped file
//----------------------------------------------------------------------------------- protected void CreateOrOpenMappedFile() { try { MemoryMappedFile oMemoryMappedFile = MemoryMappedFile.CreateOrOpen("YourMemoryMappedFileName", 100); bool IsmutexCreated; Mutex oMutex = new Mutex(true, "NonPersisterMemoryMappedFilemutex", out IsmutexCreated); StreamWriter sw = new StreamWriter(oMemoryMappedFile.CreateViewStream()); sw.WriteLine("This is a Text Message to write to the memory mapped file"); sw.Close(); oMutex.ReleaseMutex(); } catch (Exception ex) { //-- Just trap this message just incase the memory file is not mapped string sMessage = ex.Message; } } //----------------------------------------------------------------------------------- //--Open the memory mapped file and read the contents //----------------------------------------------------------------------------------- protected void ReadMemoryMappedFille() { try { MemoryMappedFile oMemoryMappedFile = MemoryMappedFile.OpenExisting("YourMemoryMappedFileName"); Mutex oMutex = Mutex.OpenExisting("NonPersisterMemoryMappedFilemutex"); oMutex.WaitOne(); StreamReader sr = new StreamReader(oMemoryMappedFile.CreateViewStream()); txtRemoteMessage.Text = sr.ReadLine(); sr.Close(); oMutex.ReleaseMutex(); } catch (Exception ex) { //-- Just trap this message just incase the memory file is not mapped string sMessage = ex.Message; } }

 Now lets see it work

Download Source Code ( VS 2012 )