c++ - 如何通过引用从 MQL4 向 C++ DLL 传递参数

标签 c++ dll mql4

我正在尝试在 MQL4 中编写一个简单的程序,该程序通过引用将变量传递给 C++ DLL 函数,并在 MQL4 中打印更新后的变量值。下面给出的是我的代码。

动态链接库函数:

void Test(int* X)
{
    *X = 6;
}

MQL4 代码

#import "Test.dll"
void Test(int&);
#import

void OnStart()
{
  int A;
  Test(A);
  Alert(A);
}

但是我没有从变量 A 的 C++ 函数中得到任何值。有人可以帮我解决我在这里做错的问题吗?

提前致谢

最佳答案

让我们从 DLL 端开始:

int TestMoreApproachesAtONCE( int *X,
                              int *Y,
                              int  Z
                              )
{
    *X = 6;                   // 6 assigned to a 4B-memory chunk ref'd by *X
     Y = 6;                   // 6 assigned to a variable  Y
     return( Z );             // Z returned as a value passed from Caller
}

MQL4 要求 DLL 具有:

Functions imported DLL into a mql4-program must ensure the Windows API calls agreement. To ensure such an agreement, in the source text of programs written in C or C++, use the keyword __stdcall, which is specific to the Microsoft(r) compilers. This agreement is characterized by the following:

· caller (in our case it is a mql4-program) should "see" a prototype of a function called (imported from the DLL), in order to properly combine parameters to a stack;

· caller (in our case it is a mql4-program) puts parameters to the stack in a reverse order, from right to left - in this order an imported function reads parameters passed to it;

· parameters are passed by value, except those explicitly passed by reference (in our case strings)

· an imported function cleans the stack independently by reading parameters passed to it.

When describing the prototype of an imported function, default parameters can be used.

If the corresponding library is unable to load, or there is a prohibition on the DLL use, or the imported function is not found - the Expert Advisor stops its operation with the appropriate message "Expert Advisor stopped" in the Journal (log file). In this case the Expert Advisor will not run until it is reinitialized. An Expert Advisor can be reinitialized as a result of recompilation or after the table of its properties is opened and OK is pressed.

现在演示 MQL4 端:

#import    "Test.dll" // -----------------------------------------------

                         void Test( int& );

                         int  TestMoreApproachesAtONCE( int &X,
                                                        int &Y,
                                                        int  Z
                                                        );
#import // "Test.dll" // -----------------------------------------------    

void OnStart()
{
     int A = EMPTY,
         B = EMPTY,
         C = EMPTY;
  // ---------------------------------------------------<PRE>
     Print( " TEST:: inital values are: A = ", A,
                                      " B = ", B,
                                      " C = ", C
                                      );
  // ---------------------------------------------------<TEST>

     C = TestMoreApproachesAtONCE( A, B, 6 );

  // ---------------------------------------------------<POST>
     Print( " TEST::  final values are: A = ", A,
                                      " B = ", B,
                                      " C = ", C
                                      );

}

Anyway, enjoy the Wild Worlds of MQL4 -- Also may enjoy to click and read other posts on issues in MQL4/DLL integration and/or signalling/messaging in MQL4 domains. Feel free to ask more

最后,MQL4 文档指出:

Passing Parameters
All parameters of simple types are passed by values unless it is explicitly indicated that they are passed by reference. When a string is passed, the address of the buffer of the copied string is passed; if a string is passed by reference, the address of the buffer of this string without copying it is passed to the function imported from DLL.

Structures that contain dynamic arrays, strings, classes, other complex structures, as well as static or dynamic arrays of the enumerated objects, can't be passed as a parameter to an imported function.

When passing an array to DLL, the address of the beginning of the data buffer is always passed (irrespective of the AS_SERIES flag). A function inside a DLL knows nothing about the AS_SERIES flag, the passed array is a static array of an undefined length; an additional parameter should be used for specifying the array size.

关于c++ - 如何通过引用从 MQL4 向 C++ DLL 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520941/

相关文章:

node.js - 如何将多个时间范围数据从 MQL4 发送到 Node.js?

wininet - 从 URL 读取文件到字符串缓冲区的简单函数(使用 WinInet.dll 的 C++/MQL{4|5})

c++ - 将 ptr 显式转换为 "ptr to a ptr"

Windows Loader 对 DLL 的操作

C# 将 DLL 限制为只有一个实例

java - 使用 Java 向设备发送命令

c++ - 如何保护 DLL 功能,或获取导入的位置?

C++,错误不匹配运算符<<

c++ - 在 C++ 中将字节序列重复到更大缓冲区的最简单方法

c++ - vector<unique_ptr> 上的 is_copy_constructible 误报