title: Named pipe client (c++) with c# server
body: <p>What I want to do is have a named pipe client written in c++ to be able to communicate with a named pipe server written in C#. I have been unable to accomplish this so far.</p>
  
  <p>CreateFile gives me an invalid handle value, GetLastError returns 2.</p>
  
  <p>Here is the c++ part (client)</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt; 
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  #define PIPE_NAME L"\\\\.\\pipe\\TestPipe"
  #define BUFF_SIZE 512
  
  int main()
  {
      HANDLE hPipe;
  
      hPipe = CreateFile(PIPE_NAME, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
  
      if (hPipe == INVALID_HANDLE_VALUE)
      {
          cout &lt;&lt; "INVALID_HANDLE_VALUE" &lt;&lt; GetLastError() &lt;&lt; endl;
          cin.get();
          return -1;
      }
  
      cout &lt;&lt; hPipe &lt;&lt; endl;
  
      DWORD mode = PIPE_READMODE_MESSAGE;
  
      SetNamedPipeHandleState(hPipe, &amp;mode, nullptr, nullptr);
  
      bool success = false;
      DWORD read;
  
      while(true)
      {
          TCHAR chBuff[BUFF_SIZE];
          do
          {
              success = ReadFile(hPipe, chBuff, BUFF_SIZE*sizeof(TCHAR), &amp;read, nullptr);
          } while (!success);
  
          _tprintf(TEXT("\"%s\"\n"), chBuff);
      }
  }
  </code></pre>
  
  <p>And here is the server</p>
  
  <pre><code>using System;
  using System.IO.Pipes;
  using System.Text;
  
  namespace BasicServer
  {
  
      public static class Program
      {
          private static NamedPipeServerStream _server;
  
          static void Main(string[] args)
          {
              _server = new NamedPipeServerStream(@"\\.\pipe\TestPipe", PipeDirection.Out, 1, PipeTransmissionMode.Message);
              _server.WaitForConnection();
              Console.WriteLine(_server.IsConnected);
              Console.WriteLine("Client connected\n Sending message");
              byte[] buff = Encoding.UTF8.GetBytes("Test message");
              _server.Write(buff, 0, buff.Length);
              while (true)
              {
                  Console.ReadKey();
                  Console.Write("\b \b");
              }
          }
      }
  }
  </code></pre>
  
  <p>I have been able to connect with a client written in C# but the c++ -> C# communication should be possible as far as I know.</p>
  
  <p>Here was my test client I wrote in c#, which works</p>
  
  <pre><code>using System;
  using System.IO.Pipes;
  using System.Text;
  
  namespace BasicClientC
  {
  
      public class Program
      {
          private static NamedPipeClientStream client;
  
          static void Main(string[] args)
          {
              client = new NamedPipeClientStream(@".", @"\\.\pipe\TestPipe", PipeDirection.In);
              client.Connect();
              byte[] buffer = new byte[512];
              client.Read(buffer, 0, 512);
              Console.WriteLine(Encoding.UTF8.GetString(buffer));
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  
  <p>So where is my mistake?</p>