Search code examples
c#dllmql4

How to create a dll function by reference in C# accepting values from MQL4 and returning it?


Here is the original function:

    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;

    }

Here is what I tried:

public  static  void GetEvent ([MarshalAs (UnmanagedType.I4)] int event_n, [MarshalAs (UnmanagedType.LPTStr)] ref  string   el_name, [MarshalAs (UnmanagedType.I4)] ref  int id, [MarshalAs (UnmanagedType.I8)] ref  long lparam, [MarshalAs (UnmanagedType.R8)] ref  double dparam, [MarshalAs (UnmanagedType.LPTStr)] ref  string sparam)
        {
            GuiEvent e = m_global_events [event_n];
            el_name = e.el_name;
            Console.WriteLine (e.el_name);
            id = ( int ) e.id;
            lparam = e.lparam;
            dparam = e.dparam;
            sparam = e.sparam;
        }

I get an error:

Access violation  

Please let me know what I can do to make it work with MQL4.

Dll access like this:

#import    "my.dll"  
void GetEvent ( int index, string element_name, int elemnet_id, long element_lparam, double element_dparam, string element_sparam);
#import

Solution

  • I guess I can fix your problem.
    Here it is:
    First, lets fix your C# Dll Function:
    It has to be something like this:

    [DllExport("GetEvent", CallingConvention = CallingConvention.StdCall)]
    public static void GetEvent([MarshalAs(UnmanagedType.I4)] int event_n, [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder el_name, [In, Out, MarshalAs(UnmanagedType.I4)] ref int el_id,
        [In, Out, MarshalAs(UnmanagedType.I8)]ref long lparam, [In, Out, MarshalAs(UnmanagedType.R8)]ref double dparam, [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder sparam)
    {
        GuiEvent e = m_global_events[event_n];
        _ = el_name.Replace("-1", e.el_name);
        el_id = (int)e.id;
        lparam = e.lparam;
        dparam = e.dparam;
        _ = sparam.Replace("-1", e.sparam);
    }
    

    Now after fixing the C# function, lets improve your MQL4:

    #import    "my.dll"  
    void GetEvent ( int index, string &element_name, int &elemnet_id, long &element_lparam, double &element_dparam, string &element_sparam);
    #import
    

    Now to access the values you have to call the function with something like this:

    void OnInit()
    {
    string name="-1", sparam="-1";
          int id;
          long lparam;
          double dparam;
          GetEvent(0, name, id, lparam, dparam, sparam);
          PrintFormat("name: %s, id: %d, lparam: %ld, dparam: %lf, sparam: %s",name, id, lparam, dparam, sparam);
    }
    

    I hope this will help you. And here are some of the references that will help you in future: Reference: https://www.mql5.com/en/articles/249, https://www.mql5.com/en/forum/150219, https://www.mql5.com/en/forum/149586