Click here to Skip to main content
Click here to Skip to main content

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.

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.

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.

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.

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.

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralThank you Pinmembertomas2185-Jun-14 21:00 
QuestionHELP: C++ can't send SPACE [modified] PinmemberBilal Rabbani5-Feb-14 1:47 
BugRunning the C++ code in release mode always returns true from readfile [modified] PinmemberMatija Koželj23-Nov-13 4:30 
GeneralRe: Running the C++ code in release mode always returns true from readfile PinmemberMember 105350852-Feb-14 22:29 
GeneralRe: Running the C++ code in release mode always returns true from readfile Pinmemberwmywipured3-Feb-14 17:56 
GeneralRe: Running the C++ code in release mode always returns true from readfile PinmemberBilal Rabbani4-Feb-14 3:44 
GeneralRe: Running the C++ code in release mode always returns true from readfile [modified] PinmemberBilal Rabbani4-Feb-14 3:46 
QuestionPrograms do not end gracefully Pinmembermichael_lin016-Mar-13 5:19 
SuggestionRe: Programs do not end gracefully PinmemberMatija Koželj25-Dec-13 8:16 
BugVery nice starting point PinmemberStéphane Lenclud7-Feb-13 3:18 
However one could argue there are some serious flaws in there.
I would certainly not recommend anyone to use this as is in production environment.
Then again I'm a C# and Named Pipe newbie so maybe I'm missing something.
 
Just a couple of my concerns:
- Since you use non-blocking mode and you simply loop around the C++ client ends up consuming near 100% of your CPU for naught. (The server too maybe?)
- ListenForClients() creates a pipe on every loop iteration that does not seem right.
 
Hope this does come out too harsh.
I'm very thankful you published this article.
GeneralRe: Very nice starting point PinmemberAmir Hesami7-Feb-13 12:33 
QuestionThank You! PinmemberNeal175921-Sep-12 13:42 
AnswerRe: Thank You! PinmemberAmir Hesami22-Sep-12 13:08 
AnswerRe: Thank You! PinmemberApertureShrooms1-Dec-12 18:47 
AnswerRe: Thank You! PinmemberAdrianJB5-Jul-13 2: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 | Mobile
Web02 | 2.8.140827.1 | Last Updated 13 Jul 2012
Article Copyright 2012 by Amir Hesami
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid