title: C# c++ named pipes connection
body: <p>I am trying to connect a c# project with a c++ project using named pipes but the c++ project doesn't connect.</p>
  
  <p>ps: the .exe are both in the same file</p>
  
  <p>side question: i don't understand the use of "\\.\pipe\" before my pipe name. what does it do and it is really necesarry?</p>
  
  <p>Here's the my code maybe you can spot the error</p>
  
  <p>C# server:</p>
  
  <p>Program.cs</p>
  
  <pre><code>static class Program
  {
  
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
  
          pipeHandler pipe = new pipeHandler();
  
          var proc = new Process();
          proc.StartInfo.FileName = "cplpltestpipes.exe";
          proc.Start();
  
  
          pipe.establishConnection();
  
          Application.Run(new Form1(pipe));
      }
  }
  
  
  public class pipeHandler
  {
  
      private StreamReader re;
      private StreamWriter wr;
      private NamedPipeServerStream pipeServer;
  
      public void establishConnection()
      {
         pipeServer = new NamedPipeServerStream("myNamedPipe1");
          pipeServer.WaitForConnection();
          re = new StreamReader(pipeServer);
          wr = new StreamWriter(pipeServer);
      }
  
      public void writePipe(string text)
      {
          wr.Write(text);
  
      }
  
      public string readPipe()
      {
          if(re.Peek()==-1)
              System.Threading.Thread.Sleep(2000);
          if (re.Peek() &gt; -1)
          {
              string s;
              s = re.ReadToEnd();
              return s;
          }
          else
              return "fail";
      }
  
  
  }
  </code></pre>
  
  <p>Form1.cs:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      pipeHandler pipePointer;
      public Form1(pipeHandler pipe)
      {
          pipePointer=pipe;
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          pipePointer.writePipe(textBox1.Text);
          textBox2.Text = pipePointer.readPipe();
      }
  
  
  }
  </code></pre>
  
  <p>c++ client</p>
  
  <pre><code>#define chrSize 16
  
  int main()
  {
      TCHAR chr[chrSize];
      DWORD bytesRead;
  
  
  
  HANDLE pipeHandler;
  LPTSTR pipeName = TEXT("\\\\.\\pipe\\myNamedPipe1");
  
  
  pipeHandler = CreateFile(
      pipeName,   // pipe name 
      GENERIC_READ |  // read and write access 
      GENERIC_WRITE,
      0,              // no sharing 
      NULL,           // default security attributes
      OPEN_EXISTING,  // opens existing pipe 
      0,              // default attributes 
      NULL);          // no template file 
  
  bool flag=false;
  
  while (!flag) 
  {
      flag = ConnectNamedPipe(pipeHandler, NULL);
      cout &lt;&lt; "trying";
  }
  
  
  
  ReadFile(
      pipeHandler,    // pipe handle 
      chr,    // buffer to receive reply 
      chrSize * sizeof(TCHAR),  // size of buffer 
      &amp;bytesRead,  // number of bytes read 
      NULL);    // not overlapped 
  
  cout &lt;&lt; chr;
  
  
  LPTSTR pipeMessage = TEXT("message receive");
  DWORD bytesToWrite= (lstrlen(pipeMessage) + 1) * sizeof(TCHAR);
  DWORD cbWritten;
  
  
  WriteFile(
      pipeHandler,                  // pipe handle 
      pipeMessage,             // message 
      bytesToWrite,              // message length 
      &amp;cbWritten,             // bytes written 
      NULL);                  // not overlapped 
  
  
  CloseHandle(pipeHandler);
  </code></pre>
  
  <p>}</p>
  
  <p>Running the program just gives this exception in C#</p>
  
  <p>************** Exception Text **************
  System.InvalidOperationException: Pipe hasn't been connected yet.
  ....
  ....
  ....</p>
  
  <p>and in c++ just keeps printing "trying" in console</p>