Click here to Skip to main content
13,862,011 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 13 Jul 2012
Licenced CPOL

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

, 13 Jul 2012
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

You may also be interested in...


Comments and Discussions

Questionexample code is no more available Pin
n3o rld3-Sep-16 7:38
membern3o rld3-Sep-16 7:38 
QuestionHelp!! Could not read or write from named pipe when created on a different thread Pin
Member 114951255-May-15 2:19
memberMember 114951255-May-15 2:19 
GeneralThank you Pin
tomas2185-Jun-14 22:00
membertomas2185-Jun-14 22:00 
QuestionHELP: C++ can't send SPACE Pin
Bilal Rabbani5-Feb-14 2:47
memberBilal Rabbani5-Feb-14 2:47 
BugRunning the C++ code in release mode always returns true from readfile Pin
Matija Koželj23-Nov-13 5:30
memberMatija Koželj23-Nov-13 5:30 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
jimmyWangmy2-Feb-14 23:29
memberjimmyWangmy2-Feb-14 23:29 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
wmywipured3-Feb-14 18:56
memberwmywipured3-Feb-14 18:56 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
Bilal Rabbani4-Feb-14 4:44
memberBilal Rabbani4-Feb-14 4:44 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
Bilal Rabbani4-Feb-14 4:46
memberBilal Rabbani4-Feb-14 4:46 
QuestionPrograms do not end gracefully Pin
michael_lin016-Mar-13 6:19
membermichael_lin016-Mar-13 6:19 
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 
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    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 13 Jul 2012
Article Copyright 2012 by Amir Hesami
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid