<!-- 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,
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
class CProcess1Dlg : public CDialog
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)
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
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.
strcpy(buf, "Hello other process..\r\n");
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.