6

I want to send data from a c# application to a c++ application through a pipe. Here is what I've done:

this is the c++ client:

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[]) {

  HANDLE hFile;
  BOOL flg;
  DWORD dwWrite;
  char szPipeUpdate[200];
  hFile = CreateFile(L"\\\\.\\pipe\\BvrPipe", GENERIC_WRITE,
                             0, NULL, OPEN_EXISTING,
                             0, NULL);

  strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe");
  if(hFile == INVALID_HANDLE_VALUE)
  { 
      DWORD dw = GetLastError();
      printf("CreateFile failed for Named Pipe client\n:" );
  }
  else
  {
      flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
      if (FALSE == flg)
      {
         printf("WriteFile failed for Named Pipe client\n");
      }
      else
      {
         printf("WriteFile succeeded for Named Pipe client\n");
      }
      CloseHandle(hFile);
  }
return 0;

}

and here the c# server

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace PipeApplication1{

class ProgramPipeTest
{

    public void ThreadStartServer()
    {
        // Create a name pipe
        using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("\\\\.\\pipe\\BvrPipe"))
        {
            Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());

            // Wait for a connection
            pipeStream.WaitForConnection();
            Console.WriteLine("[Server] Pipe connection established");

            using (StreamReader sr = new StreamReader(pipeStream))
            {
                string temp;
                // We read a line from the pipe and print it together with the current time
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("{0}: {1}", DateTime.Now, temp);
                }
            }
        }

        Console.WriteLine("Connection lost");
    }

    static void Main(string[] args)
    {
        ProgramPipeTest Server = new ProgramPipeTest();

        Thread ServerThread = new Thread(Server.ThreadStartServer);

        ServerThread.Start();

    }
}

}

when I start the server and then the client GetLastErrror from client returns 2 (The system cannot find the file specified.)

Any idea on this. Thanks

itchy
  • 63
  • 1
  • 1
  • 3

2 Answers2

6

At a guess, I'd say you don't need the "\.\Pipe\" prefix when creating the pipe in the server. The examples of calling the NamedPipeServerStream constructor that I've seen just pass-in the pipe name. E.g.

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("BvrPipe"))

You can list the open pipes, and their names, using SysInternals process explorer. This should help you verify that the pipe has the correct name. See this question for details.

Community
  • 1
  • 1
Andy Johnson
  • 7,938
  • 4
  • 33
  • 50
  • I can confirm this, I have used pipes between C# and C++, and I did not use the Pipe\ prefix on the pipe name when creating a NamedPipeServerStream. – Bob Moore Oct 05 '16 at 10:52
  • In the above Code, till we call CloseHandle on pipe handle from C++ code, data is not passed to C# Server . Is there any way to pass without closing handle ? I tried "FlushFileBuffers" function also. It dint work – Raveendra M Pai Feb 27 '17 at 05:23
  • @Raveendra You can't flush a socket. See http://stackoverflow.com/questions/12814835/flush-a-socket-in-c for info. Also see http://www.tangentsoft.net/wskfaq/intermediate.html#packetscheme. – Andy Johnson Feb 28 '17 at 11:32
-1

Please read this to see how the pipes are implemented. Why are you not using CreateNamedPipes API call? You are treating the file handle on the C++ side as an ordinary file not a pipe. Hence your C++ code fails when it is looking for a pipe when in fact it was attempting to read from a file.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 1
    It is perfectly valid to use CreateFile() to open the _client_ end of a pipe. See the Remarks section for the CreateFile function (http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx). – Andy Johnson Dec 15 '09 at 11:35
  • @andyjohnson: I wasn't aware that you could do that...my bad...sorry for the misleading statement... :( – t0mm13b Dec 15 '09 at 11:58