Click here to Skip to main content
15,867,901 members
Articles / Programming Languages / C++
Article

Synchronous Interprocess communication, A wrapper: Part I

Rate me:
Please Sign up or sign in to vote.
4.36/5 (10 votes)
25 Apr 20032 min read 66K   1.6K   41   2
A class to demonstrate another approach to IPC

Introduction

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.

Background

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:

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:

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:
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()

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));    
  m_ctrlText.SetWindowText(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.

License

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


Written By
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 Microsoft and Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.

Elias currently works as an Anticheat engineer in Blizzard Entertainment.

Elias co-authored 2 books and authored one book:

- Practical Reverse Engineering
- The Antivirus Hacker's Handbook
- The Art of Batch Files Programming

Comments and Discussions

 
GeneralA few comments... Pin
peterchen8-Apr-03 11:59
peterchen8-Apr-03 11:59 
GeneralRe: A few comments... Pin
Elias Bachaalany10-Apr-03 20:57
Elias Bachaalany10-Apr-03 20: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.