title: IPC between C# and C++ using named pipes
body: <p>I have a C++ server application and C# client application, and I want them to communicate with each other using named pipes mechanism.</p>
  
  <p>I want to use a multithreded server (each tread will service a client), I found a working code here: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx</a></p>
  
  <p>It works great with the C++ client from here: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx</a></p>
  
  <p>My goal is to make this C++ server to work with C# client, so I wrote this simple program for the client side:</p>
  
  <pre><code>public static void Main(string[] Args)
  {
      //Client
      var client = new NamedPipeClientStream(".", "mynamedpipe", PipeDirection.InOut);
      client.Connect();
  
      StreamReader reader = new StreamReader(client, Encoding.Unicode);
      StreamWriter writer = new StreamWriter(client, Encoding.Unicode);
      int i = 0;
      while (true)
      {
          writer.WriteLine("Message" + i.ToString() + Char.MinValue);
          writer.Flush();
          i++;
  
          System.Console.WriteLine(reader.ReadLine());
  
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>The behavior I expect is :</p>
  
  <pre><code>Client-&gt;Server: "Message1"
  Server-&gt;Client: "default answer from server"
  Client-&gt;Server: "Message2"
  Server-&gt;Client: "default answer from server"
  Client-&gt;Server: "Message3"
  Server-&gt;Client: "default answer from server"
  Client-&gt;Server: "Message4"
  Server-&gt;Client: "default answer from server"
  </code></pre>
  
  <p>And so on...</p>
  
  <p>Instead, this is the output I get:</p>
  
  <blockquote>
    <p>Pipe Server: Main thread awaiting client connection on \.\pipe\mynamedpipe 
    Client connected, creating a processing thread.</p>
    
    <p>Pipe Server: Main thread awaiting client connection on \.\pipe\mynamedpipe
    InstanceThread created, receiving and processing messages.</p>
    
    <p>Client Request String:"?????????????????????????????????????????????????????????
    ????????????????????????????????????????????????????????????????????????????????
    ????????????????????????????????????????????????????????????????????????????????
    ????????????????????????????????????????????????????????????????????????????????
    ????????????????????????????????????????????????????????????????????????????????
    ????????????????????????????????????????????????????????????????????????????????
    ???????????????????????????????????????????????????????????"</p>
    
    <p>Client Request String:"Message0"</p>
  </blockquote>
  
  <p>After some debugging here are few weird things I noticed:</p>
  
  <ol>
  <li>client.Connect(); causes 2 bytes (not null terminated)  to be written to the pipe. this explains the "?????..."</li>
  <li>"Messege0" gets written correctly from the client side and the server reads it correctly also, but:</li>
  <li>Client side does not seem to read the reply from the server ("default answer from server") correctly - it is stuck on the blocking "reader.ReadLine()" line. I do not think it is because of a bug on the server as the same server seems to work fine with the C++ client.</li>
  <li><p>After closing the server, the message gets to the client. I added </p>
  
  <p>FlushFileBuffers(hPipe);</p></li>
  </ol>
  
  <p>after</p>
  
  <pre><code>WriteFile()  
  </code></pre>
  
  <p>in the server - it did not help.</p>
  
  <ul>
  <li><p>Encoding seems to be correct on both sides - Unicode, Little Endien.</p></li>
  <li><p>Both server and client running on the same Windows machine.</p></li>
  </ul>
  
  <p>The issue in item number 3, is the most disturbing to me, and I cannot find an explanation.</p>