title: Named Pipe C# client can't connect to C++ server
body: <p>I'm trying to get a C++ application to let a C# application know when a particular action happens. The way I'm trying to do this is via named pipes.</p>
  
  <p>I've set up a named pipe server on the C++ app, which seems to be working (the named pipe gets created - it appears on the list retrieved by <a href="https://technet.microsoft.com/en-us/sysinternals/dd581625.aspx" rel="nofollow noreferrer">PipeList</a>) and a named pipe client on the C# app, where it fails: First line of the C# client code gives "Pipe handle has not been set. Did your PipeStream implementation call InitializeHandle?" error, and line 2 throws "Access to the path is denied" exception.</p>
  
  <p>Where am I going wrong?</p>
  
  <p><strong>C++ Server Code</strong></p>
  
  <pre><code>CString namedPipeName = "\\\\.\\pipe\\TitleChangePipe";
  
  HANDLE pipe = CreateNamedPipe(namedPipeName, PIPE_ACCESS_INBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
  if (pipe == INVALID_HANDLE_VALUE) {
      MessageBox(NULL, "Pipe Could Not be Established.", "Error: TCM", MB_ICONERROR);
      return -1;
  }
  
  char line[512]; DWORD numRead;
  
  while (true)//just keep doing this
  {
      numRead = 1;
      while ((numRead &lt; 10 || numRead &gt; 511) &amp;&amp; numRead &gt; 0)
      {
          if (!ReadFile(pipe, line, 512, &amp;numRead, NULL) || numRead &lt; 1) {//Blocking call
              CloseHandle(pipe);                                          //If something went wrong, reset pipe
              pipe = CreateNamedPipe(namedPipeName, PIPE_ACCESS_INBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
              ConnectNamedPipe(pipe, NULL);
              if (pipe == INVALID_HANDLE_VALUE) {
                  MessageBox(NULL, "Pipe Could Not be Established.", "Error: TCM", MB_ICONERROR);
                  return -1; }
              numRead = 1;
          }
      }
      line[numRead] = '\0';   //Terminate String
  }   
  
  CloseHandle(pipe);
  </code></pre>
  
  <p><strong>C# Client Code</strong></p>
  
  <pre><code>var client = new NamedPipeClientStream(".", "TitleChangePipe", PipeDirection.InOut);
  client.Connect();
  var reader = new StreamReader(client);
  var writer = new StreamWriter(client);
  
  while (true)
  {
      var input = Console.ReadLine();
      if (String.IsNullOrEmpty(input))
           break;
      writer.WriteLine(input);
      writer.Flush();
      Console.WriteLine(reader.ReadLine());
  }
  </code></pre>