How to read and write message between C# and MQL5 using named pipe

 

I am trying to make a communication channel between C# named pipe server and MQL5 named pipe client. Named pipe server and client is being created successfully and also client connected to server. From client send message to server and from server read the data successfully. But the problem is when send message from server to client, then client can't read the data. Or not sure that server successfully send the data or not.

Here is the server source which create a C# Named pipe server.

namespace PipeServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!!!");

            using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("TestPipeServer", PipeDirection.InOut, 1, PipeTransmissionMode.Byte))
            {
                Console.WriteLine("NamedPipeServerStream object created.");

                // Wait for a client to connect
                Console.Write("Waiting for client connection...");
                pipeServer.WaitForConnection();
                Console.WriteLine("Client connected.");

                Console.WriteLine("Read client's response");
                const int BUFFERSIZE = 256;
                bool completed = false;
                while (!completed)
                {
                    byte[] buffer = new byte[BUFFERSIZE];
                    int nRead = pipeServer.Read(buffer, 0, BUFFERSIZE);
                    string line = Encoding.UTF8.GetString(buffer, 0, nRead);
                    Console.WriteLine(line);
                    if (line == "bye")
                        completed = true;
                }
                Console.WriteLine("Send response");
                byte[] msgAck = Encoding.UTF8.GetBytes("ACK from server");
                pipeServer.Write(msgAck, 0, msgAck.Length);
                pipeServer.Flush();
                pipeServer.WaitForPipeDrain();
            }

            Console.WriteLine("\npress any key to exit the process...");
            // basic use of "Console.ReadKey()" method
            Console.ReadKey();
        }
    }
}

Here is the client source which is written in MQL5 for MT5 as EA

//+------------------------------------------------------------------+
//|                                                       TestEA.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Files\FilePipe.mqh>

CFilePipe  ExtPipe;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(10);

   while(!IsStopped())
     {
      if(ExtPipe.Open("\\\\.\\pipe\\TestPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE)
         break;
      Sleep(250);
     }
   Print("Client: pipe opened");
//--- send welcome message
   if(!ExtPipe.WriteString(__FILE__+" on MQL5 build "+IntegerToString(__MQ5BUILD__)))
     {
      Print("Client: sending welcome message failed");
      return(INIT_FAILED);
     }
   else
     {
      ExtPipe.WriteString("bye");
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   ExtPipe.Close();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//printf(__FUNCTION__+": method called");
   MqlTick last_tick;
   string sendStr;
   if(SymbolInfoTick(Symbol(), last_tick))
     {
      sendStr = StringFormat("Symbol: %s, Bid: %f, Ask: %f", Symbol(), last_tick.bid, last_tick.ask);
      //printf(sendStr);
     }
   else
     {
      printf("SymbolInfoTick() failed, error = ",GetLastError());
     }
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//printf(__FUNCTION__ + ": method called");
//--- read data from server
   string str;
   if(!ExtPipe.ReadString(str))
     {
      Print("Client: reading string failed");
      return;
     }
   Print("Server: ",str," received");
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---

  }
//+------------------------------------------------------------------+
Have Googled a bit but not find any appropriate solution. If there is any similar question, please share. Thanks.

 

Hi

This forum actually doesnt help so much...I have the same problem, many days tryng to implement mt5 with VS2019 in 64bits and ZERO help...

Good luck

Reason: