Click here to Skip to main content
13,287,834 members (36,943 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 5 Apr 2003

Synchronous Interprocess communication, A wrapper: Part I

, 25 Apr 2003
Rate this:
Please Sign up or sign in to vote.
A class to demonstrate another approach to IPC
<!-- Add the rest of your HTML here -->


In this article I will show yet another approach to IPC. I will demonstrate how the memory mapped files and couple of synchronization objects can be used to accomplish a Client/Server like interprocess communication.

Memory mapped file (swap file backed) is used as the shared memory medium. A mutex object is used to synchronize access to the shared memory.

Then two events are used to handle send/recv. which makes this possible: Client or server cannot send and receive from itself nor can either one of them exist alone.

This same Class can tell whether its current instance is a client or a server by checking who first initialzed the memory mapped file and other internal objects.


You might have already used other IPC method, but when you will use this class you will notice that within two lines of code you will have two processes communicating together smoothly.

Multiple sends will be satisfied only when receive because I am not currently queueing buffers. Last sent buffer is the buffer that will be received.

Using the code

You can start using the code by simply initiating an instance of this class on both of your applications. In order to make them communicate together just specify a unique channel name for each processes pair. The CInterProcessCommunication constructor is defined as: <PRE lang=c++>CInterProcessCommunication::CInterProcessCommunication( LPCTSTR SharedName, DWORD Size, BOOL bHandlesInheritable)

The SharedName is the channel name that will be used by these two processes. The Size is the size of the biggest buffer that will be interchanged. The bHandlesInheritable is an optional parameter, you can ignore it.

Now include the class interface in your program and declare an instance: <PRE lang=c++>// Process1Dlg.h : header file // . . . ///////////////////////////////////////////////////////////////////////////// // CProcess1Dlg dialog #include "../interprocesscommunication.h" class CProcess1Dlg : public CDialog { // Construction public: CInterProcessCommunication m_ipc; CProcess1Dlg(CWnd* pParent = NULL); // standard constructor . . . Now initiate the instance by specifying a channel and max size: <PRE lang=c++>// Process1Dlg.cpp : implementation file // . . . . CProcess1Dlg::CProcess1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CProcess1Dlg::IDD, pParent), m_ipc("mychannel", 1024) { //{{AFX_DATA_INIT(CProcess1Dlg)

Now that you have defined the communication means, you still have to connect. In order to connect, another instance of CInterProcessCommunication must be initiated (following the same process as described above).

Now you can connect using the InterConnect() <PRE lang=c++>void CProcess1Dlg::OnBtnConnect() { // try to connect if (!m_ipc.InterConnect()) { MessageBox("Could not connect, probably no other instance is ready for connection"); } } Start the communication using the blocking mode methods void CInterProcessCommunication::ReceiveBuffer(LPVOID Buffer, DWORD Size) void CInterProcessCommunication::SendBuffer(LPVOID Buffer, DWORD Size) The receive method, when called and there is nothing to receive it will block execution until it receives something.

void CProcess1Dlg::OnBtnSayHello() 
  char buf[100];
  strcpy(buf, "Hello other process..\r\n");
  m_ipc.SendBuffer(buf, strlen(buf)+1);

void CProcess1Dlg::OnBtnReadReply() 
  char buf[100];

  // wait until you receive a reply.
  m_ipc.ReceiveBuffer(buf, sizeof(buf));    
In the attached sources, follow this sequence to see a demo: Process1: press "Connect" Process2: press "Connect" Process1: press "Say Hello" Process1: press "Read reply" Process2: press "Receive salutation" Process2: press "Send reply"

Points of Interest

I wanted to pass buffers from processes to process easily and in a blocking (synchronous) matter. I had to learn about other IPC methods and as I discovered that they do not fit my needs I wrote this class. After all, hope you like it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Elias Bachaalany
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.

His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.

In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.

Former employee of Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.

Elias currently works at Microsoft as a software security engineer.

More articles and blog posts can be found here:


You may also be interested in...

Comments and Discussions

GeneralA few comments... Pin
peterchen8-Apr-03 12:59
memberpeterchen8-Apr-03 12:59 
GeneralRe: A few comments... Pin
lallous10-Apr-03 21:57
memberlallous10-Apr-03 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 26 Apr 2003
Article Copyright 2003 by Elias Bachaalany
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid