How communicate with MT4 from DLL in c#?

 

How communicate with MT4 from DLL in c#?

                          (USDCHF)
                               MT4
                                 ^
                                  |
                                  v
(USDCHF) MT4 <--> Dll C# <--> MT4 (GBPUSD)
                                  ^
                                  |
                                  v
                               MT4

                           (USDCAD)


PS: The code below in c# and mt4 is good for communicate with DLL from MT4, but in the other way doesn't work

Thank you in advance for your help.

Best,

Fabiano

////////////////////////////////////////////////////////////////////////////////////////////
///////// CODE C# //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

public class CommonCoreDLL_x86
{

[DllExport("CommunicationWithMT4formDLL", CallingConvention = CallingConvention.Cdecl)]
public static void CommunicationWithMT4formDLL(){
 Task.Factory.StartNew(() =>
 {
    NamedPipeServerStream pipeServer = new NamedPipeServerStream("PipelineName", PipeDirection.InOut, 254, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
    int threadId = Thread.CurrentThread.ManagedThreadId;
    pipeServer.WaitForConnection();

    while (true)
        {
            try
            {

                if (isConnectedToMt4 == false)
                {
                    break;
                }

                pipeServer.WaitForPipeDrain();

                byte[] byteRec;
                byteRec = new byte[256];

                pipeServer.Read(byteRec, 0, 256);

                List<byte> listWeNeed = new List<byte>();
                foreach (byte b in byteRec)
                {
                    if (b != '\0')
                    {
                        listWeNeed.Add(b);
                    }
                }

                byte[] byteFinally = listWeNeed.ToArray();

                string strReaded = Encoding.UTF8.GetString(byteFinally);

                if (strReaded.Length > 2)
                {
            MessageBox.Show(strReaded);
            // Communication with MT4 from here
            // CommunicationWithDLLfromMT4();
            // ?????????????????????????????
        }else{
            isConnectedToMt4 == false;
        }

        }
        catch (IOException e)
                {
            isConnectedToMt4 == false;
        }
    }

  });
}

[DllExport("GetDateTimeToString", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPTStr)]
public static string GetDateTimeToString()
     {
       return DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
     }

}

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////
//////////////// Code MT4 //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

#import "C:/Users/*****/AppData/Roaming/MetaQuotes/Terminal/50CA3DFB510CC5A8F28B48D1BF2A5702/MQL4/Experts/Library/CommonCoreDLL_x86.dll"
    void CommunicationWithMT4formDLL();
    string GetDateTimeToString();
#import

#include <Files\FilePipe.mqh>
CFilePipe ExtPipe;

int OnInit(){
   CommunicationWithMT4formDLL();
}

int start(){
    if(ExtPipe.Open("\\\\.\\pipe\\PipelineName",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE)
         {
        if(!ExtPipe.WriteString("test write"){
            Alert("problem");
        }else{
            Alert(GetDateTimeToString());
        }
         }
  return(0);
}

void CommunicationWithDLLfromMT4(){
   Alert("good communication");
}

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////

 

In Visual Studio, import the Unmanaged Exports nuget package into your project (https://www.nuget.org/packages/UnmanagedExports).

Then all you have to do is decorate your functions properly:

[DllExport]
public static int setSymbolAverage(double Open, double Close, [MarshalAs(UnmanagedType.LPWStr)]string Symbol)
        {
            return ((Open+Close)/2);
        }

 I included how to send in a string also.

 

The 2 usings you'll need to:

using System.Runtime.InteropServices;
using RGiesecke.DllExport;
Reason: