Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C++

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  
A class to demonstrate another approach to IPC
// Process1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Process1.h"
#include "Process1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CProcess1Dlg dialog

CProcess1Dlg::CProcess1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProcess1Dlg::IDD, pParent), m_ipc("mychannel", 1024)
{
	//{{AFX_DATA_INIT(CProcess1Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProcess1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProcess1Dlg)
	DDX_Control(pDX, IDC_TXT_RECEIVE, m_ctrlText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProcess1Dlg, CDialog)
	//{{AFX_MSG_MAP(CProcess1Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnBtnConnect)
	ON_BN_CLICKED(IDC_BUTTON3, OnBtnSayHello)
	ON_BN_CLICKED(IDC_BUTTON4, OnBtnReadReply)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProcess1Dlg message handlers

BOOL CProcess1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CProcess1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CProcess1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CProcess1Dlg::OnBtnConnect() 
{
  // try to connect
  if (!m_ipc.InterConnect())
  {
    MessageBox("Could not connect, probably no other instance is ready for connection");
  }	
  else MessageBox("Connected!");
}

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); 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for 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