Named Pipe communication between two terminals

 

Dear Community,

I try using the Named Pipes to exchange data between two terminals. Unfortunately, I can not get it to work.. I've already read most of the entries in this forum regarding Named Pipes but found nothing suitable.

I can not find the error or problem in my code. I use the script "MyPipeWriter" in first terminal  and the script "MyPipeReader" in the second terminal.


The function:

ExtPipe.Open("\\\\.\\pipe\\MQL4.Pipe.MyPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)

delivers all the time an invalid handle.

I would be very grateful if someone could give me a advice what I'm doing wrong. I try this in the MT4.

Thank you for your help!


//+------------------------------------------------------------------+
//|                                                 MyPipeWriter.mq4 |
//|                   Copyright 2012-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2012-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property version     "1.00"
#property description "Pipe client sample using CFilePipe standard class"
#property strict

#include <Files\FilePipe.mqh>

CFilePipe ExtPipe;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
//--- wait for pipe server
   bool bfirst=true;
   while(!IsStopped()){

      if(ExtPipe.Open("\\\\.\\pipe\\MQL4.Pipe.MyPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE){
         Print("Test1");
         break;
      }
      
      if(bfirst){
         bfirst=false;
         Print("Client: waiting for pipe server");
      }
      
      Sleep(250);
   }
   
   if(IsStopped())
      return;
   
   Print("Client: pipe opened");
//--- send welcome message
   
   if(!ExtPipe.WriteString(__FILE__+" on MQL4 build "+IntegerToString(__MQL4BUILD__))){
      Print("Client: sending welcome message failed");
      return;
   }

   ExtPipe.Close();
}




//+------------------------------------------------------------------+
//|                                                 MyPipeReader.mq4 |
//|                   Copyright 2012-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2012-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property version     "1.00"
#property description "Pipe client sample using CFilePipe standard class"
#property strict

#include <Files\FilePipe.mqh>

CFilePipe ExtPipe;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
//--- wait for pipe server
   bool bfirst=true;
   while(!IsStopped()){
      if(ExtPipe.Open("\\\\.\\pipe\\MQL4.Pipe.MyPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)==INVALID_HANDLE)
         break;
      
      if(bfirst){
         bfirst=false;
         Print("Client: waiting for pipe server");
      }
      Print("Test2");
      Sleep(250);
   }
   
   if(IsStopped())
      return;
   
   Print("Client: pipe opened");

//--- read data from server
   string str;
   int    value=0;

   if(!ExtPipe.ReadString(str))
     {
      Print("Client: reading string failed");
      return;
     }
   Print("Server: \"",str,"\" received");

   if(!ExtPipe.ReadInteger(value))
     {
      Print("Client: reading integer failed");
      return;
     }
   Print("Server: ",value," received");
//---
   ExtPipe.Close();
}


Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs
Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs
  • 2012.10.15
  • MetaQuotes Software Corp.
  • www.mql5.com
Many developers face the same problem - how to get to the trading terminal sandbox without using unsafe DLLs. One of the easiest and safest method is to use standard Named Pipes that work as normal file operations. They allow you to organize interprocessor client-server communication between programs. Take a look at practical examples in C++ and MQL5 that include server, client, data exchange between them and performance benchmark.
Files:
 

user_123 is right,  using recent MT4 builds, ExtPipe.Open function ( <Files\FilePipe.mqh> provided and included in MT4 ) always return INVALID_HANDLE


Why leaving  <Files\FilePipe.mqh> included file if they are not working anymore with latest build ?

 
kariboo92:

user_123 is right,  using recent MT4 builds, ExtPipe.Open function ( <Files\FilePipe.mqh> provided and included in MT4 ) always return INVALID_HANDLE


Why leaving  <Files\FilePipe.mqh> included file if they are not working anymore with latest build ?

You can't use MT4/MT5 as a server, only as a client.
Reason: