Click here to Skip to main content
6,293,171 members and growing! (12,917 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Inter-Process Communication     Intermediate

Synchronous Interprocess communication, A wrapper: Part I

By lallous

A class to demonstrate another approach to IPC
VC6, VC7Win2K, WinXP, Dev
Posted:5 Apr 2003
Updated:25 Apr 2003
Views:41,408
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
9 votes for this article.
Popularity: 3.89 Rating: 4.08 out of 5

1

2
2 votes, 22.2%
3
3 votes, 33.3%
4
4 votes, 44.4%
5

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:

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:

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

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

About the Author

lallous


Member
Elias (aka lallous) 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.

Elias maintains a blog at http://lallousx86.wordpress.com/ and a website at http://lgwm.org/
Occupation: Web Developer
Location: Lebanon Lebanon

Other popular Threads, Processes & IPC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralA few comments... Pinsupporterpeterchen12:59 8 Apr '03  
GeneralRe: A few comments... Pinmemberlallous21:57 10 Apr '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Apr 2003
Editor: Nick Parker
Copyright 2003 by lallous
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project