Click here to Skip to main content
15,886,816 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.3K   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:	CppNamedPipeClient.cpp
* Project:		CppNamedPipeClient
* Copyright (c) Microsoft Corporation.
* 
* Named pipe is a mechanism for one-way or bi-directional inter-process 
* communication between the pipe server and one or more pipe clients in the
* local machine or across the computers in the intranet:
* 
* PIPE_ACCESS_INBOUND:
* Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
* 
* PIPE_ACCESS_OUTBOUND:
* Client (GENERIC_READ) <--- Server (GENERIC_WRITE)
* 
* PIPE_ACCESS_DUPLEX:
* Client (GENERIC_READ or GENERIC_WRITE, or both) <--> 
* Server (GENERIC_READ and GENERIC_WRITE)
* 
* This sample demonstrates a named pipe client that attempts to connect to   
* the pipe server, \\.\pipe\HelloWorld, with the GENERIC_READ and 
* GENERIC_WRITE permissions. The client writes a message to the pipe server 
* and receive its response.
* 
* 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/11/2009 11:20 PM Jialiang Ge Created
\***************************************************************************/

#pragma region Includes
#include "stdafx.h"

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


#define BUFFER_SIZE		1024 // 1K

int _tmain(int argc, _TCHAR* argv[])
{
	/////////////////////////////////////////////////////////////////////////
	// Try to open a named pipe.
	// 

	// Prepare the pipe name
	CString strPipeName;
	strPipeName.Format(_T("\\\\%s\\pipe\\%s"), 
		_T("."),			// Server name
		_T("HelloWorld")	// Pipe name
		);
	
	HANDLE hPipe; 
	while (TRUE) 
	{
		hPipe = CreateFile( 
			strPipeName,			// Pipe name 
			GENERIC_READ |			// Read and write access 
			GENERIC_WRITE,
			0,						// No sharing 
			NULL,					// Default security attributes
			OPEN_EXISTING,			// Opens existing pipe 
			0,						// Default attributes 
			NULL);					// No template file 

		// Break if the pipe handle is valid. 
		if (hPipe != INVALID_HANDLE_VALUE) 
			break; 
 
		if (// Exit if an error other than ERROR_PIPE_BUSY occurs
			GetLastError() != ERROR_PIPE_BUSY 
			||
			// All pipe instances are busy, so wait for 5 seconds
			!WaitNamedPipe(strPipeName, 5000)) 
		{
			_tprintf(_T("Unable to open named pipe %s w/err 0x%08lx\n"),
				strPipeName, GetLastError());
			return 1;
		}
	}
	_tprintf(_T("The named pipe, %s, is connected.\n"), strPipeName);
	

	/////////////////////////////////////////////////////////////////////////
	// Sets the read mode and the blocking mode of the specified named pipe.
	// 

	// Set data to be read from the pipe as a stream of messages
	DWORD dwMode = PIPE_READMODE_MESSAGE;
	BOOL bResult = SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL);
	if (!bResult) 
	{
		_tprintf(_T("SetNamedPipeHandleState failed w/err 0x%08lx\n"), 
			GetLastError()); 
		return 1;
	}


	/////////////////////////////////////////////////////////////////////////
	// Send a message to the pipe server and receive its response.
	// 

	// A char buffer of BUFFER_SIZE chars, aka BUFFER_SIZE * sizeof(TCHAR) 
	// bytes. The buffer should be big enough for ONE request to the server.

	TCHAR chRequest[BUFFER_SIZE];	// Client -> Server
	DWORD cbBytesWritten, cbRequestBytes;
	TCHAR chReply[BUFFER_SIZE];		// Server -> Client
	DWORD cbBytesRead, cbReplyBytes;

	// Send one message to the pipe.

	StringCchCopy(chRequest, BUFFER_SIZE, _T("Default request from client"));
	cbRequestBytes = sizeof(TCHAR) * (lstrlen(chRequest) + 1);

	bResult = WriteFile(			// Write to the pipe.
		hPipe,						// Handle of the pipe
		chRequest,					// Message to be written
		cbRequestBytes,				// Number of bytes to write
		&cbBytesWritten,			// Number of bytes written
		NULL);						// Not overlapped 

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

	_tprintf(_T("Sends %ld bytes; Message: \"%s\"\n"), 
		cbBytesWritten, chRequest);

	// Receive the response from the server.

	cbReplyBytes = sizeof(TCHAR) * BUFFER_SIZE;
	do
	{
		bResult = ReadFile(			// Read from the pipe.
			hPipe,					// Handle of the pipe
			chReply,				// Buffer to receive the reply
			cbReplyBytes,			// Size of buffer 
			&cbBytesRead,			// Number of bytes read 
			NULL);					// Not overlapped 

		if (!bResult && GetLastError() != ERROR_MORE_DATA) 
		{
			_tprintf(_T("ReadFile failed w/err 0x%08lx\n"), GetLastError());
			break;
		}

		_tprintf(_T("Receives %ld bytes; Message: \"%s\"\n"), 
			cbBytesRead, chReply);

	} while (!bResult);  // Repeat loop if ERROR_MORE_DATA 


	/////////////////////////////////////////////////////////////////////////
	// Close the pipe.
	// 

	CloseHandle(hPipe); 

	return 0;
}

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