Click here to Skip to main content
15,896,490 members
Articles / Desktop Programming / Win32

Inter-Process Communication (IPC) Introduction and Sample Code

Rate me:
Please Sign up or sign in to vote.
4.91/5 (57 votes)
19 Dec 2009Ms-PL8 min read 251.6K   12.2K   195  
This article will cover general IPC technologies in All-In-One Code Framework. The IPC technologies include Named Pipes, File Mapping, MailSlot, etc.
/****************************** Module Header ******************************\
* Module Name:	CppMailslotClient.cpp
* Project:		CppMailslotClient
* Copyright (c) Microsoft Corporation.
* 
* Mailslot is a mechanism for one-way inter-process communication in the 
* local machine or across the computers in the intranet. Any clients can  
* store messages in a mailslot. The creator of the slot, i.e. the server,  
* retrieves the messages that are stored there:
* 
* Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
* 
* This sample demonstrates a mailslot client that connects and writes to the 
* mailslot \\.\mailslot\HelloWorld. 
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 1/21/2009 11:04 PM Jialiang Ge Created
\***************************************************************************/

#pragma region Includes
#include "stdafx.h"

#include <windows.h>
#include <atlstr.h>
#include <strsafe.h>
#pragma endregion


BOOL WriteMailslot(HANDLE hSlot, LPTSTR lpszMessage);


int _tmain(int argc, _TCHAR* argv[])
{
	/////////////////////////////////////////////////////////////////////////
	// Open the mailslot.
	// 

	// Prepare the slot name
	CString strSlotName;
	strSlotName.Format(_T("\\\\%s\\mailslot\\%s"), 
		_T("."),			// Server name
		_T("HelloWorld")	// Pipe name
		);

	HANDLE hFile = CreateFile(
		strSlotName,				// The name of the mailslot
		GENERIC_WRITE,				// Write access (Mailslot is a mechanism 
									// for one-way IPC. The client is just
									// responsible for writing to the slot)
		FILE_SHARE_READ,			// Share mode
		NULL,						// Default security attributes
		OPEN_EXISTING,				// Opens existing mailslot 
		FILE_ATTRIBUTE_NORMAL,		// The file has no other attributes set
		NULL);						// No template file

	if (hFile == INVALID_HANDLE_VALUE) 
	{
		_tprintf(_T("Unable to open mailslot %s w/err 0x%08lx\n"),
			strSlotName, GetLastError());
		return 1;
	}


	/////////////////////////////////////////////////////////////////////////
	// Write messages to the mailslot.
	// 

	WriteMailslot(hFile, _T("Message 1 for mailslot"));
	WriteMailslot(hFile, _T("Message 2 for mailslot"));

	Sleep(3000); // Sleep 3 seconds

	WriteMailslot(hFile, _T("Message 3 for mailslot"));


	/////////////////////////////////////////////////////////////////////////
	// Close the slot.
	// 

	CloseHandle(hFile); 

	return 0;
}


/*!
 * \brief
 * Write a message to the specified mailslot.
 * 
 * \param hSlot
 * The handle of the mailslot
 * 
 * \param lpszMessage
 * The message to be written to the slot
 * 
 * \returns
 * If the function succeeds, the return value is nonzero.
 * If the function fails, the return value is zero. To get extended error 
 * information, call GetLastError.
 */
BOOL WriteMailslot(HANDLE hSlot, LPTSTR lpszMessage)
{
	DWORD cbMessageBytes = 0;		// Size of the message in bytes
	DWORD cbBytesWritten = 0;		// Number of bytes written to the slot

	BOOL bResult; 

	// Calculate the size of the message in bytes
	cbMessageBytes = (lstrlen(lpszMessage) + 1) * sizeof(TCHAR);

	bResult = WriteFile(			// Write to the mailslot.
		hSlot,						// Handle of the slot
		lpszMessage,				// Message to be written
		cbMessageBytes,				// Number of bytes to write
		&cbBytesWritten,			// Number of bytes written
		NULL); 						// Not overlapped 

	if (!bResult/*Failed*/ || cbMessageBytes != cbBytesWritten/*Failed*/) 
	{ 
		_tprintf(_T("WriteFile failed w/err 0x%08lx\n"), GetLastError());
		return FALSE; 
	}

	_tprintf(_T("The message, %s, is written to the slot.\n"), 
		lpszMessage); 

	return TRUE;
}

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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
China China
Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.
http://cfx.codeplex.com/

Comments and Discussions