Named Pipe communication between two terminals

 

Dear Community,

sorry for the double post. I ask the question at the www.mql5.com forum, but there nobody could help me. Maybe somebody here have an answer for me.

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.

Thank you for your help!


//+------------------------------------------------------------------+
//|                                                 MyPipeWriter.mq4 |
//|                   Copyright 2012-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2012-2014, MetaQuotes Software Corp."
#property link        "https://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. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2012-2014, MetaQuotes Software Corp."
#property link        "https://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();
}
Files:
 

1) Can you post File.mqh?

2) Why is it placed in \Files and not in \Include?

3) Is the mql5-code due to the dot in the function-name?

 

Hello Gooly,

thank you for your help:

1) See attached Files

2) The FilePipe.mqh is located in the folder "MetaTrader4/MQL4/Include/Files/". "Files" is here only a subfolder in the "Include" folder. The FilePipe.mqh and the File.mqh are by default in the MetaTrader4 and are from MetaQuotes.

3) Could you explain me what you mean by "the dot in the function-name"?


Thank You

Files:
filepipe.mqh  13 kb
file.mqh  12 kb
 

I was once created a pipe between 2 terminals (before b600+).

I used "NamedPipeServer.mq4" and  "NamedPipeClient.mq4" with its Windows' kernel32.dll functions.
 
user_123: 3) Could you explain me what you mean by "the dot in the function-name"?
CFilePipe ExtPipe; if(ExtPipe.Open( ...
  1. There can be no dots in function or variable names since build 600 (February 3, 2014.) Dots are now used to access structure and class elements. Structures and Classes - MQL4 Documentation
  2. Functions are stand alone. Not part of a class. FileOpen - MQL4 Documentation is a function. CFilePipe is a class (no dot there.) Open() is a CFilePips class method (no dot there.)
  3. ExtPipe is a variable (no dot there.) ExtPips.Open(...) is a call of Open() method of the CFilePipe class, using the ExtPipe class instance. Instance dot method = instance access element.
  4. Learn about OOP Oriented Programming - MQL4 Documentation
 
I did not find the command CreateNamedPipe anywhere in your code. How do you initialize your pipe?
 
WHRoeder:
  1. There can be no dots in function or variable names since build 600 (February 3, 2014.) Dots are now used to access structure and class elements. Structures and Classes - MQL4 Documentation
  2. Functions are stand alone. Not part of a class. FileOpen - MQL4 Documentation is a function. CFilePipe is a class (no dot there.) Open() is a CFilePips class method (no dot there.)
  3. ExtPipe is a variable (no dot there.) ExtPips.Open(...) is a call of Open() method of the CFilePipe class, using the ExtPipe class instance. Instance dot method = instance access element.
  4. Learn about OOP Oriented Programming - MQL4 Documentation

Hello WHRoeder I know what the dot method means. For me it was not clear what Gooly means by the 3) question.. I think Gooly means the dots in the string, but they should be no problem..

"\\\\.\\pipe\\MQL4.Pipe.MyPipeServer"
 
DeepThought:
I did not find the command CreateNamedPipe anywhere in your code. How do you initialize your pipe?

Hello DeepThought,

meanwhile I have found a library, which set up a named pipe with the kernel32.dll. This library also contains the CreateNamedPipe command.


Is it possible to use the Named Pipes without DLLs?

 
user_123:

Hello DeepThought,

meanwhileI have found a library, which set up a named pipe with the kernel32.dll. This library also contains the CreateNamedPipe command.


Is it possible to use the Named Pipes without DLLs?

To be honest, I do not know if it can be achieved without DLL. I got tired from testing what works properly with the current IO functions and currently I rely completely on Winapi.

Regarding pipes, I use asynchronous mode (the one with the OVERLAPPED structure), because the simple synchronous mode can very easily block entire MT4Terminal in case of some unexpected event happens (which happens often during development).