metatrader5 eventhänling


Project description:


The DLL Can be loaded into MT5, the public static functions from the project dll can be called too. Now we need to implement some event handling in mt5 – like for the Cloud Connection Status Update.



Problem description:


In the DLL is a function named Authenticate() – this function is triggering the event , which must be triggered down to our mt5 Expert Advisor.



This is the Authenticate function


 [DllExport("Authenticate", CallingConvention = CallingConvention.StdCall)]

        [return: MarshalAs(UnmanagedType.Bool)]

        public static bool Authenticate([MarshalAs(UnmanagedType.LPWStr)] string username)




  //do some stuff



            catch (Exception ex)



                throw ex;


            return true;







This is the event implementation in the DLL:


private static void RaiseConnectionUpdate(string Status)


            RaiseConnectionStateUpdateEvent("StatusEvent", new XTConnectionStateUpdateEvent(Status));






public class XTConnectionStateUpdateEvent : EventArgs


            public XTConnectionStateUpdateEvent(string Status) : base() { connectionstate = Status; }


            private string connectionstate;

            public string ConnectionState


                get { return connectionstate; }

                set { connectionstate = value; }






public static event EventHandler<XTConnectionStateUpdateEvent> RaiseConnectionStateUpdateEvent;

        static void OnRaiseConnectionStateUpdateEvent(XTConnectionStateUpdateEvent e)


            EventHandler<XTConnectionStateUpdateEvent> handler = RaiseConnectionStateUpdateEvent;


            // Event will be null if there are no subscribers

            if (handler != null)



                // Use the () operator to raise the event.

                handler(null, e);






Before authenticate() can be called, we need to setup the event listening -

In mql5 we need these c# functions:


public void AddEventListeners(){

RTGNinjaCloud.RaiseConnectionStateUpdateEvent += XTHandler_RaiseConnectionStateUpdateEvent;



private void XTHandler_RaiseConnectionStateUpdateEvent(object sender, RTGNinjaCloud.XTConnectionStateUpdateEvent e)


Print  “done”;




I have tried some code like this:


typedef int (__stdcall * Callback)(const char* text);

Callback Handler = 0;

extern "C" __declspec(dllexport)

void __stdcall SetCallback(Callback handler) {

  Handler = handler;


extern "C" __declspec(dllexport)

void __stdcall TestCallback() {

  int retval = Handler("hello world");





…that needs some .h files to include:

#include "stdafx.h"

#include "iostream.h"


But these files cannot be found..


can't open "C:\Users\ccs\AppData\Roaming\MetaQuotes\Terminal\65D1CFCD3F6A1A06FDD09AF8B01AA7EF\MQL5\Experts\RTG\stdafx.h" include file        


Need these files to be downloaded each?

Is there a way to install/use those precompiled headers?

What is the mt5 code for this simple event handling?




If is your header file is in the same folder like your program (script, EA or indicator) then include file must have quotation marks (#include "stdafx.mqh").

If is it in another folder (include or libraries) then it must be in angle brackets (#include <stdafx.mqh>)

Your includes should be located in the includes folder to avoid programming issues.

Hope this solves your problem....

Documentation on MQL5: Language Basics / Preprocessor / Including Files (#include)
Documentation on MQL5: Language Basics / Preprocessor / Including Files (#include)
Language Basics / Preprocessor / Including Files (#include) - Reference on algorithmic/automated trading language for MetaTrader 5
Btw: This isn't German ;-)