title: Named Pipes C# Server C++ .dll Client Not Working?
body: <p>So I'm trying to use Named Pipes to communicate between a C# server and C++ .dll client that I will inject.
  My named pipes works fine when used with a C++ Console App but when I use the same method in the .dll the Server and Client connect fine but when I try use NamedPipeServerStream.Write() my C# app freezes. They are both x64 if that matters at all.</p>
  
  <p>Any ideas as to why?</p>
  
  <p>C# Server</p>
  
  <pre><code>    Thread ServerThread;
      private static NamedPipeServerStream _server;
  
      private void Form1_Load(object sender, EventArgs e)
      {
          ServerThread = new Thread(ServerThreadSub);
  
          ServerThread.Start();
      }
  
      void ServerThreadSub()
      {
          while (true)
          {
              _server = new NamedPipeServerStream("ConsolePipe", PipeDirection.Out, 1, PipeTransmissionMode.Message);
              _server.WaitForConnection();
              do
              {
              } while (_server.IsConnected);
              _server.Close();
          }
      }
  
      private void text_CurrentCommand_KeyDown(object sender, KeyEventArgs e)
      {
          if (e.KeyCode == Keys.Return)
          {
              if (text_CurrentCommand.Text != "")
              {
                  if (_server.IsConnected)
                  {
                      try
                      {
                          byte[] buff = Encoding.UTF8.GetBytes(text_CurrentCommand.Text);
                          _server.Write(buff, 0, buff.Length); //Freezes here
                      }
                      catch
                      {
                          Log("Client Disconnected, Command Not Processed.");
                      }
                  }
                  else
                  {
                      Log("Command Not Processed. Client Not Connected.");
                  }
              }
              e.SuppressKeyPress = true;
          }
      }
  </code></pre>
  
  <p>C++ Client</p>
  
  <pre><code>#define PIPE_NAME L"\\\\.\\pipe\\ConsolePipe"
  #define BUFF_SIZE 1024
  
  HANDLE hPipe;
  
  DWORD WINAPI Main(LPVOID threadArgs)
  {
      while (true)
      {
          do
          {
              Sleep(1000);
              hPipe = CreateFile(PIPE_NAME, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
              if (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE) DeleteFile(PIPE_NAME);
          } while (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE);
  
          DWORD mode = PIPE_READMODE_MESSAGE;
  
          SetNamedPipeHandleState(hPipe, &amp;mode, nullptr, nullptr);
  
          bool success = false;
          DWORD read;
  
          while (true)
          {
              char chBuff[BUFF_SIZE];
              success = ReadFile(hPipe, chBuff, (DWORD)strlen(chBuff), &amp;read, nullptr);
              if (success)
              {
              }
              if (!success) break;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>Also another query I have that isn't really important, is there any way to get NamedPipeServerStream.IsConnected to refresh without performing a Read() or Write()?</p>
  
  <p>Thanks in advance c:</p>