Discussion of article "Developing graphical interfaces for Expert Advisors and indicators based on .Net Framework and C#" - page 10

 
awesome! works like a charm. thank you very much!
 
Can this library be used with MT4 also ? 
 
Can we attach this .Net app into chart?
 

For MT4, We need to do some changes on MtGuiController.dll. like DLLEXPORT using RGiesecke.DllExport and marshalling input & output parameters.

 // add reference using nuget 
using RGiesecke.DllExport;

        [DllExport("ShowForm", CallingConvention = CallingConvention.StdCall)]
        public static void ShowForm([MarshalAs(UnmanagedType.LPWStr)] string assembly_path, [MarshalAs(UnmanagedType.LPWStr)] string form_name)
        {
            try
            {
                GuiController controller = GetGuiController(assembly_path, form_name);
                string full_path = assembly_path + "/" + form_name;
                m_controllers.Add(full_path, controller);
                controller.RunForm();
            }
            catch(ArgumentException e)
            {
                Type t = e.GetType();
                SendExceptionEvent(e);
            }
        }
        [DllExport("HideForm", CallingConvention = CallingConvention.StdCall)]
        public static void HideForm([MarshalAs(UnmanagedType.LPWStr)] string assembly_path, [MarshalAs(UnmanagedType.LPWStr)] string form_name)
        {
            try
            {
                string full_path = assembly_path + "/" + form_name;
                if (!m_controllers.ContainsKey(full_path))
                    return;
                GuiController controller = m_controllers[full_path];
                controller.DisposeForm();
                m_controllers.Remove(full_path);
            }
            catch(Exception ex)
            {
                SendExceptionEvent(ex);
            }
        }
        [DllExport("SendEvent", CallingConvention = CallingConvention.StdCall)]
        public static void SendEvent([MarshalAs(UnmanagedType.LPWStr)] string el_name, [MarshalAs(UnmanagedType.U4)] int id, [MarshalAs(UnmanagedType.U8)] long lparam, [MarshalAs(UnmanagedType.R8)] double dparam, [MarshalAs(UnmanagedType.LPWStr)] string sparam)
        {            
            SendEventRef(el_name, ref id, ref lparam, ref dparam, sparam);
        }
        [DllExport("ReceiveEvent", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.LPWStr)]
        public static string ReceiveEvent([MarshalAs(UnmanagedType.I4)] int event_n)
        {
            string json = "";
            int id=0;
            string el_name="", sparam="";
            long lparam=0;
            double dparam=0.0;
            GetEvent(event_n,ref el_name, ref id, ref lparam, ref dparam, ref sparam);
            json = "{ \"el_name\":\"" + el_name + "\",\"id\":" + id + ",\"lparam\":" + lparam + ",\"dparam\":" + dparam + ",\"sparam\":\"" + sparam + "\" }"; 
            return json;
        }
        public static void GetEvent(int event_n, ref string el_name,ref int id,  ref long lparam, ref double dparam, ref string sparam)
        {         
                GuiEvent e = m_global_events[event_n];
                el_name = e.el_name;
                id = (int)e.id;
                lparam = e.lparam;
                dparam = e.dparam;
                sparam = e.sparam;
            
        }
       
        [DllExport("EventsTotal", CallingConvention = CallingConvention.StdCall)]
        public static int EventsTotal()
        {
            return m_global_events.Count;
        }
      


MQL4


#import  "MtGuiController.dll"
void ShowForm(string assembly_path, string form_name);
void HideForm(string assembly_path, string form_name);
void SendEvent(string el_name, int id, long lparam, double dparam, string sparam);
string ReceiveEvent(int event_n); // Returns JSON string.. Use JAson.mqh to Deserialize objects
int EventsTotal();
#import
 

I'm relative new to MQL5 and I learned a lot from this article! Thanks very much!


I have one question. Have been struggling a bit for the past 2 days to figure this out down below.


I have successfully completed each step a couple of times even on another computer but everytime when I try to compile the code I get an error message.


The MQL compiler states : "Undeclared Identifier" for any of the enumerations down below.


public enum GuiEventType
{
    Exception,
    ClickOnElement,
    TextChange,
    ScrollChange
}

If I "comment" these enumerations out from MQL5 then it works accordingly which is interesting since this means that the code works but can't read the enumerations which is my guess?

If someone can help me I would greatly appreciate :)


Thanks,

 

I am also trying to import a C# assembly. I can compile successfully, even with a call to a method imported from the assembly, so during compile time, everything seems to be fine.

But during executing in the Strategy Tester, it fails to load the EA with a simple "Cannot load 'MyDll.dll' [0]".

I put the DLL into MQL5\Libraries. I also tried putting it into the GAC, but nothing helped.

Any pointers on how to troubleshoot this?

 
dhilgarth:

I am also trying to import a C# assembly. I can compile successfully, even with a call to a method imported from the assembly, so during compile time, everything seems to be fine.

But during executing in the Strategy Tester, it fails to load the EA with a simple "Cannot load 'MyDll.dll' [0]".

I put the DLL into MQL5\Libraries. I also tried putting it into the GAC, but nothing helped.

Any pointers on how to troubleshoot this?

Call up the MQL reference in the editor (in the "Help"), go to the Search tab and enter Dll, Enter.

Then there is a page on how to import dlls (and others).

However, this article is probably better: https://www.mql5.com/en/articles/18

or this one: https://www.mql5.com/en/docs/runtime/testing#dll

Otherwise, enter tester dll in the search at the top right.
Datenaustausch: Erstellen einer DLL für MQL5 in 10 Minuten
Datenaustausch: Erstellen einer DLL für MQL5 in 10 Minuten
  • www.mql5.com
Tatsächlich erinnern sich nicht viele Entwickler daran, wie eine simple DLL-Bibliothek geschrieben wird und was die Merkmale der unterschiedlichen Systemanbindungen sind. Anhand mehrerer Beispiele werde ich versuchen, Ihnen den gesamten Prozess zur Erstellung einer simplen DLL in 10 Minuten zu zeigen, sowie einige technische Einzelheiten der...
 

was anybody able to figure out why there is below error?

The MQL compiler states : "Undeclared Identifier" for any of the enumerations down below.

I am also getting same error.

 
With controls, it's a lot easier to develop in C# compared to developing EA, and data calculations etc. have been improved, thanks for sharing.
 
sachnyc:

was anybody able to figure out why there is below error?

The MQL compiler states : "Undeclared Identifier" for any of the enumerations down below.

I am also getting same error.

You can use the numeric representation of each Event Type:

Event Type ID
Exception 0
ClickOnElement 1
TextChange 2
ScrollChange 3

Here is an example with SendEvent Function (This code is from TardePanel EA):

GuiController::SendEvent("CurrentVolume", TextChange, 0, 0.0, corr_vol);
GuiController::SendEvent("CurrentVolume", 2, 0, 0.0, corr_vol);

You change the representation of the event TextChange for its ID which is 2, and you do this for all the function that need it.