代码之家  ›  专栏  ›  技术社区  ›  Jaffer Wilson Dilip kumar

如何在C中通过引用创建dll函数,接受MQL4中的值并返回它?

  •  0
  • Jaffer Wilson Dilip kumar  · 技术社区  · 4 年前

        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;
    
        }
    

    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;
            }
    

    Access violation  
    

    请让我知道我可以做些什么来让它与MQL4一起工作。

    #import    "my.dll"  
    void GetEvent ( int index, string element_name, int elemnet_id, long element_lparam, double element_dparam, string element_sparam);
    #import
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Amazing Things Around You    4 年前

    我想我能解决你的问题。
    这里是:

    [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);
    }
    

    现在,在修复了C#函数之后,让我们改进MQL4:

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

    现在要访问这些值,您必须用如下方式调用函数:

    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);
    }
    

    参考文献: https://www.mql5.com/en/articles/249 https://www.mql5.com/en/forum/150219 , https://www.mql5.com/en/forum/149586