Click here to Skip to main content
11,485,294 members (78,022 online)
Click here to Skip to main content

Inter-Process Communication between C# and C++ using named pipes

, 13 Jul 2012 CPOL 31.6K 3.1K 18
Rate this:
Please Sign up or sign in to vote.
An example of interprocess communication using named pipes.


There might be some cases that you need to have two or more processes connected. One solution which I guess is more common is using sockets on local machine to connect process for interprocess communications. This is an example of interprocess communication using named pipes. The sample provides a code snippet for C# and C++ process communications.


Named pipes are mainly used for inter-process communications. It can be one way or duplex communication between a pipe server and one or more pipe clients. Clients can have a stream which can be used to send/receive messages between processes. Name pipes has FIFO (First - In First - Out) behaviour.

Using the code

Two named pipes are created in this example. You have to run the C# application to initialise the pipes. Pipes are named myNamedPipe1 and myNamedPipe2, where in the C# application myNamedPipe1 is used for receiving and myNamedPipe2 for sending, and in C++ application myNamedPipe1 is used for sending and myNamedPipe2 for receiving.

A class called NamedPipeServer is used in C# code to create instances of namedpipes. Servers should be started after creating an instance of them and stopped when the application is closed. A Named pipe exists beyond the life of the process and must be deleted after the process is closed.

NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0);
NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1);
string Ms="Start";
   Console.WriteLine("Enter the message");
   Ms = Console.ReadLine();
   PServer2.SendMessage(Ms, PServer2.clientse);
} while (Ms != "quit");

This snipped keeps sending messages until it gets quit command, then it closes both named pipes.  Same name pipes with the exact same name are created in the C++ code and initialise.

LPTSTR lpszPipename1 = TEXT("\\\\.\\pipe\\myNamedPipe1"); 
LPTSTR lpszPipename2 = TEXT("\\\\.\\pipe\\myNamedPipe2");  

Same as C# code, a thread processes received messages, and a loop in the main thread sends messages to the other application.

    printf ("Enter your message: ");
    scanf ("%s",buf);  
    if(strcmp (buf,"quit") == 0)
        WriteFile(hPipe1, buf, dwBytesToWrite, &cbWritten, NULL);

After running both applications you can send/receive messages between processes.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amir Hesami
Software Developer (Senior)
Australia Australia
No Biography provided

Comments and Discussions

SuggestionRe: Programs do not end gracefully Pin
Matija Koželj25-Dec-13 9:16
memberMatija Koželj25-Dec-13 9:16 
BugVery nice starting point Pin
Stéphane Lenclud7-Feb-13 4:18
memberStéphane Lenclud7-Feb-13 4:18 
GeneralRe: Very nice starting point Pin
Amir Hesami7-Feb-13 13:33
memberAmir Hesami7-Feb-13 13:33 
QuestionThank You! Pin
Neal175921-Sep-12 14:42
memberNeal175921-Sep-12 14:42 
Thank you for posting this. I needed to flip/flop the client & server code where my server is in c++ and my client is in c#, but I was able to port your code & it works well. It does a nice job handling clients as they come & go.
AnswerRe: Thank You! Pin
Amir Hesami22-Sep-12 14:08
memberAmir Hesami22-Sep-12 14:08 
AnswerRe: Thank You! Pin
ApertureShrooms1-Dec-12 19:47
memberApertureShrooms1-Dec-12 19:47 
AnswerRe: Thank You! Pin
AdrianJB5-Jul-13 3:57
memberAdrianJB5-Jul-13 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 13 Jul 2012
Article Copyright 2012 by Amir Hesami
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid