Code for mql4 Client PIPE

 

Hi everybody.

I use this code in my .net app:

Call in load or button:

Listen("Pipe1")

Private _pipeName As String

 
    Public Sub Listen(PipeName As String)
        Try
            ' Set to class level var so we can re-use in the async callback method
            _pipeName = PipeName
            ' Create the new async pipe 
            Dim pipeServer As New NamedPipeServerStream(PipeName, PipeDirection.[In], 1, PipeTransmissionMode.[Byte], PipeOptions.Asynchronous)

            ' Wait for a connection
            pipeServer.BeginWaitForConnection(New AsyncCallback(AddressOf WaitForConnectionCallBack), pipeServer)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub WaitForConnectionCallBack(iar As IAsyncResult)
        Try
            ' Get the pipe
            Dim pipeServer As NamedPipeServerStream = DirectCast(iar.AsyncState, NamedPipeServerStream)
            ' End waiting for the connection
            pipeServer.EndWaitForConnection(iar)

            Dim buffer As Byte() = New Byte(254) {}

            ' Read the incoming message
            pipeServer.Read(buffer, 0, 255)

            ' Convert byte buffer to string
            Dim stringData As String = Encoding.UTF8.GetString(buffer, 0, buffer.Length)


            ' Pass message back to calling form
            MsgBox(stringData)

            ' Kill original sever and create new wait server
            pipeServer.Close()
            pipeServer = Nothing
            pipeServer = New NamedPipeServerStream(_pipeName, PipeDirection.[In], 1, PipeTransmissionMode.[Byte], PipeOptions.Asynchronous)

            ' Recursively wait for the connection again and again....
            pipeServer.BeginWaitForConnection(New AsyncCallback(AddressOf WaitForConnectionCallBack), pipeServer)
        Catch ex As Exception
            MsgBox(ex.Message)
            Return
        End Try
    End Sub

With an client in .net too, this code function perfect.

But I want to connect the mql4 as client in my .net app.

Who knows how can I make this? I try many exemples, include others format like pipe = "\\\\.\\pipe\\"+PipeName; many types of codes but anything connect my app. I try an exemple in in source code here, this code not generate errror, but I dont receive anything in my app.

Thanks all.

 
This code not generate any error. But anything happens.
//+------------------------------------------------------------------+
//|                                                        teste.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#define NMPWAIT_USE_DEFAULT_WAIT   0
#define NMPWAIT_NOWAIT 1

#include <stderror.mqh>
#include <stdlib.mqh>

#import "kernel32.dll"
   int CallNamedPipeA(string PipeName, string outBuffer, int outBufferSz, int& inBuffer[], int inBufferSz, int& bytesRead[], int timeOut);
#import

extern string PipeName = "\\\\.\\pipe\\Pipe1";
//extern string PipeName = "Pipe1";
extern string broker = "broker2";
extern color lineColor = Red;
string pipe;
int err;
string sendmessage(string message)
{
   int inBuffer[256];
   int bytesRead[1];
   
   bool check = CallNamedPipeA(
                    pipe,
                    message,
                    StringLen(message)+1,
                    inBuffer,
                    4*ArraySize(inBuffer),
                    bytesRead,
                    3000);           
   if (bytesRead[0] != 0)
    { 
     
     message = "";        
     for(int i=0; i<bytesRead[0]; i++)
      message = message + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
    
     return (message);
    } else{
    err=GetLastError();
    Print("error(",err,"): ",ErrorDescription(err));

    }                
   return ("");
}
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   //pipe = "\\\\.\\pipe\\"+PipeName;
   pipe = PipeName;
   string returned = sendmessage("Add:" + broker +";"+lineColor);
   Print(returned);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   string returned = sendmessage("Remove:" + broker);
   Print(returned);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   string returned = sendmessage(broker + ";" + Bid + ";");
   Print(returned);
   return(0);
  }
//+------------------------------------------------------------------+
 

I use the method NamedPipeServerStream because this offer async method in vb.net. So, my app can receive quotes in realtime from mt4 without DDE and to continue with all others functions functioning perfcet.

If you knows other way, thanks.

sds

 

With this code in VB.net:

Private Sub CriaPipe()
        Dim openMode, pipeMode As Integer
        openMode = PIPE_ACCESS_DUPLEX Or FILE_FLAG_WRITE_THROUGH
        pipeMode = PIPE_WAIT Or PIPE_TYPE_MESSAGE Or PIPE_READMODE_MESSAGE
        hPipe = CreateNamedPipe(pipeName, openMode, pipeMode, 10, 10000, 2000, 10000, IntPtr.Zero)
    End Sub
    'fica na espera que o mt4 se conecte para enviar informações
    Private Sub WaitPipeClients()
        Dim byteCount, i, res, cbnCount As Integer
        For i = 0 To BUFFSIZE - 1 'Fill an array of numbers
            Buffer(i) = i Mod 256
        Next i
        'Wait for a connection, block until a client connects

        Me.Refresh()
        Do
            res = ConnectNamedPipe(hPipe, 0)
            'Read the data sent by the client over the pipe
            cbnCount = 4
            res = ReadFile(hPipe, byteCount, Len(byteCount), cbnCount, 0)
            If byteCount > BUFFSIZE Then 'Client requested for byteCount bytes
                byteCount = BUFFSIZE 'but only send up to 20000 bytes
            End If
            'Write the number of bytes requested by the client 
            res = WriteFile(hPipe, Buffer, byteCount, cbnCount, 0)
            res = FlushFileBuffers(hPipe)
            MsgBox(res.ToString)
            'Disconnect the named pipe.
            res = DisconnectNamedPipe(hPipe)
            'Loop until the client makes no more requests for data. 
        Loop Until byteCount = 0

    End Sub

And this in mql4:

string PipeName = "\\\\.\\pipe\\"+Pipe; 
    //string PipeName = Pipe; 
    int inBuffer[256]; 
    int bytesRead[1]; 
    string sComment=""; 
    Comment("Connecting to pipe server...");  
    bool fSuccess = CallNamedPipeA(PipeName,extMessage,StringLen(extMessage)+1,inBuffer,4*ArraySize(inBuffer),bytesRead,0) != 0; 
    int lastError = GetLastError(); 
    if (fSuccess || lastError == ERROR_MORE_DATA) {  
        string inString = ""; 
        for(int i=0; i<bytesRead[0]; i++) 
            inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);         
        sComment = "Reply from server: "+inString; 
        if(!fSuccess) sComment = sComment+ "\nSome data was lost. Increase receiving buffer size.";         
        Comment(sComment);             
    } else { 
        Print("Last Error: ",lastError); 
        Comment("CallNamedPipe Failed!"); 
    } 
    
   return(0);

the connexion works, I receive messsages in my .net app but it still waiting for clients and as the connection is sync all the other function by my app not function. I need implement now, async in vb.net for works perfectly...

In code in first topic, I have the async method, but the connection not works.

Anybory want help-me?

thanks

 
bisewski:

With this code in VB.net:

And this in mql4:

the connexion works, I receive messsages in my .net app but it still waiting for clients and as the connection is sync all the other function by my app not function. I need implement now, async in vb.net for works perfectly...

In code in first topic, I have the async method, but the connection not works.

Anybory want help-me?

thanks


I think you know and have found the code to pass string from terminal to terminal or even only from chart to chart (e.g. to create a 7min chart) by a namedpipe.
What about if you try to establish with this code a namedpipe from chart to chart. Then you have a working sender and a receiver and you continue from here?
I don't use PipeServer and PipeClient, but PipeReceiver and PipeSender which better explains which one is writing to the pipe and which one is listening to the pipe.

BTW I could be that I am using a different include files as this function

CallNamedPipeA(..)

is not present. Mine are NamedPipeClient and NamedPipeServer.. I remember that it took several tries to make it work..

Gooly

 
gooly:

I think you know and have found the code to pass string from terminal to terminal or even only from chart to chart (e.g. to create a 7min chart) by a namedpipe.
What about if you try to establish with this code a namedpipe from chart to chart. Then you have a working sender and a receiver and you continue from here?
I don't use PipeServer and PipeClient, but PipeReceiver and PipeSender which better explains which one is writing to the pipe and which one is listening to the pipe.

BTW I could be that I am using a different include files as this function

is not present. Mine are NamedPipeClient and NamedPipeServer.. I remember that it took several tries to make it work..

Gooly


Thanks gooly.

Really, I work three days and nothing. This code above have errors. I need receive a message from server to send quotes to server. But I dont receive anything and the code return 0.

Well, I founded any exemples as c++ and c# pipes connections. I study more and maybe, finish this job.

 
bisewski:


Thanks gooly.

Really, I work three days and nothing. This code above have errors. I need receive a message from server to send quotes to server. But I dont receive anything and the code return 0.

Well, I founded any exemples as c++ and c# pipes connections. I study more and maybe, finish this job.


The funny is that the pipe connect ever, the problem is in data. I receive an estranger symbols, like a box and only the last char is correct.

EDIT: Oh shit...big shit...4 days...Only delete the lines FileWriteInteger(ExtPipe,size_str) and ok, works perfect.

THANKS FOR ALL THE HELP FROM YOU...