Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
13 Jul 2012CPOL1 min read 126.6K   7.4K   29   17
An example of interprocess communication using named pipes.

Introduction

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.

Background

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.

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

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.

C++
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.

C++
do
{
    printf ("Enter your message: ");
    scanf ("%s",buf);  
    if(strcmp (buf,"quit") == 0)
        Write_St=FALSE;
    else
    {
        WriteFile(hPipe1, buf, dwBytesToWrite, &cbWritten, NULL);
        memset(buf,0xCC,100);
    }
}while(Write_St);

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

Image 1

License

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


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionexample code is no more available Pin
n3o rld3-Sep-16 6:38
n3o rld3-Sep-16 6:38 
QuestionHelp!! Could not read or write from named pipe when created on a different thread Pin
Member 114951255-May-15 1:19
Member 114951255-May-15 1:19 
GeneralThank you Pin
tomas2185-Jun-14 21:00
tomas2185-Jun-14 21:00 
QuestionHELP: C++ can't send SPACE Pin
Bilal Rabbani5-Feb-14 1:47
Bilal Rabbani5-Feb-14 1:47 
BugRunning the C++ code in release mode always returns true from readfile Pin
Matija Koželj23-Nov-13 4:30
Matija Koželj23-Nov-13 4:30 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
jimmyWangmy2-Feb-14 22:29
jimmyWangmy2-Feb-14 22:29 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
wmywipured3-Feb-14 17:56
wmywipured3-Feb-14 17:56 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
Bilal Rabbani4-Feb-14 3:44
Bilal Rabbani4-Feb-14 3:44 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pin
Bilal Rabbani4-Feb-14 3:46
Bilal Rabbani4-Feb-14 3:46 
QuestionPrograms do not end gracefully Pin
michael_lin016-Mar-13 5:19
michael_lin016-Mar-13 5:19 
SuggestionRe: Programs do not end gracefully Pin
Matija Koželj25-Dec-13 8:16
Matija Koželj25-Dec-13 8:16 
BugVery nice starting point Pin
Stéphane Lenclud7-Feb-13 3:18
Stéphane Lenclud7-Feb-13 3:18 
GeneralRe: Very nice starting point Pin
Amir Hesami7-Feb-13 12:33
Amir Hesami7-Feb-13 12:33 
QuestionThank You! Pin
Neal175921-Sep-12 13:42
Neal175921-Sep-12 13:42 
AnswerRe: Thank You! Pin
Amir Hesami22-Sep-12 13:08
Amir Hesami22-Sep-12 13:08 
AnswerRe: Thank You! Pin
ApertureShrooms1-Dec-12 18:47
ApertureShrooms1-Dec-12 18:47 
AnswerRe: Thank You! Pin
AdrianJB5-Jul-13 2:57
AdrianJB5-Jul-13 2: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.