【问题标题】:How to call a function from a DLL file from [ MQL5 ] code?如何从 [MQL5] 代码中的 DLL 文件调用函数?
【发布时间】:2016-04-26 09:56:58
【问题描述】:

为什么第一个 MessageBox() 有效而第二个无效?

我不知道问题出在哪里。

MQL5 是否可以访问dll 文件?

我需要调用读取为 JSONC# 函数。

MetaEditor 中没有出现错误。

C# .dll 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestMe
{
    class Test
    {

   // [DllExport("Add", CallingConvention = CallingConvention.StdCall)]
    public static int Add(int left, int right)
    {
        return left + right;
    }


    public static int Sub(int left, int right)
    {
        return left - right;
    }


    public static double AddDouble(double left, double right)
    {
        return left + right;
    }


    public static float AddFloat(float left, float right)
    {
        return left + right;
    }
}
}

这是一个MQL5代码:

#import "TestMe.dll"
                    int    Add(       int    left, int    right );
                    int    Sub(       int    left, int    right );
                    float  AddFloat(  float  left, float  right );
                    double AddDouble( double left, double right );
#import

#property strict                 // MQL-syntax-mode-modifier == "strict"

int OnInit()
{   int k = 0;
    MessageBox( k );             // this call works
    k = Add( 1, 666 );
    MessageBox( k );             // Doesn't work

    return( INIT_SUCCEEDED );
    }

【问题讨论】:

    标签: c# algorithmic-trading mql4 mql5 metatrader5


    【解决方案1】:

    欢迎来到
    Wild
    MQL的世界

    如何测试 DLL 函数调用访问?

    这是最简单的部分。从DLL 内部对其进行测试。将打印在 stdout 上的一些输入/输出参数/值添加到每个 DLL-function 源中,在调试阶段,您将拥有所有需要的 C#-side 自诊断。

    MQL-方还需要允许所有 DLL 调用,检查 MetaTrader 终端 5 设置:
    [x] Allow DLL imports ...子>.


    语法问题:逐项检查调用签名

    MQL 文档说明了要使用的 MessageBox() 的单一、清晰的呼号:

    int MessageBox(
    string text, // message text
    string caption = NULL, // box header
    intflags = 0); // defines set of buttons in the box

    Parameters :
    text strong>: [in] 文本,包含要输出的消息。

    caption= NULL : [in] 要在框标题中显示的可选文本。如果参数为空,EA 交易名称将显示在框标题中。

    flags= 0 : [in] 定义消息框外观和行为的可选标志。标志可以是一组特殊标志的组合。 加号:默认值 == 0 == MB_OK

    Return Value :
    如果函数执行成功,返回值为MessageBox()返回码值之一。 分别是{ IDOK | IDCANCEL | IDABORT | IDRETRY | IDIGNORE | IDYES | IDNO | IDTRYAGAIN | IDCONTINUE }


    MQL 不是 C#
    MQL-string 实际上不是 string,但 struct
    MQL 不是原谅任何微小的细节:
    必须谨慎

    MQL 文档说明:

    字符串类型的内部表示是一个12字节长的结构:

    #pragma pack(push,1) 
    struct MqlString 
      { 
       int      size;       // 32-bit integer, contains size of the buffer, allocated for the string. 
       LPWSTR   buffer;     // 32-bit address of the buffer, containing the string. 
       int      reserved;   // 32-bit integer, reserved. 
      }; 
    #pragma pack(pop,1)
    

    这是 The Strange Answer 第一次调用成功的原因。
    MessageBox() 没有尝试访问它调用的任何内存位置,因为伪造的 MQL-string-struct ( ill)-通过 .size struct-component 自行声明,它自己的 .buffer 内存区域(间接寻址)长度为 0 字节,因此 没有内存区域 (根据定义,最终会发生冲突, 具有一些其他内存对象的地址空间) 将在这种特定情况下被访问。

    MQL 领域工作了十多年,拥有数百人*年的团队实践经验与爬行MQL 语言语法,我敢说,“不要依赖在编译阶段没有报告错误”MetaTrader 终端 在许多情况下使我们变得无毛,即使代码是逐字遵循已发布的文档。

    随时查看 MQL 上的其他帖子,以查看有关 DLL 集成噩梦的更多详细信息以及有关进入分布式处理的精彩故事,GPGPU-computing 等。


    关于JSON的最后评论

    如果我要设计一个架构来通过 JSON 进行通信,我会加入 ZeroMQ DLL 分布式处理服务,这将使您的目标更快,而不是构建另一个 JSON-解析器作为一个新建项目。

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      相关资源
      最近更新 更多