如何通过引用将参数从MQL4传递给C ++ DLL [英] How to pass argument by reference from MQL4 to C++ DLL

查看:230
本文介绍了如何通过引用将参数从MQL4传递给C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a simple program in MQL4 which passes a variable to a C++ DLL function by reference and prints the updated variable value in MQL4. Given below is my code.

DLL函数:

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

MQL4代码

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

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

但是我没有从C ++函数中获取任何变量A的值。有人请帮助我我在这里做错了吗?

But I am not getting any value back from C++ function for variable A. Can someone please help me what I am doing wrong here?

预先感谢

推荐答案

让我们从DLL端开始:



Let's start with the DLL-side:

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具有:



MQL4 requires DLL to have:


函数将DLL导入mql4-程序必须确保Windows API调用协议。为了确保达成这样的协议,请在使用C或C ++编写的程序的源文本中,使用关键字 __ stdcall ,该关键字特定于Microsoft( r)编译器。该协议具有以下特征:

· 呼叫者(在我们的情况下,它是mql4程序)应请参阅从DLL导入的称为函数的原型,以便将参数正确组合到堆栈中;

· 调用程序(在我们的示例中是mql4-程序)以相反的顺序从右到左将参数放入堆栈-按此顺序,导入的函数读取传递给它的参数;

· 参数是通过值传递的,但那些通过引用显式传递的参数(在我们的示例中为字符串)

· 一个导入函数,通过读取传递给它的参数来独立清理堆栈。

在描述导入函数的原型时,可以使用默认参数。

如果相应的库无法加载,或者禁止使用DLL,或者导入找不到d函数-EA停止运行,并在日志(日志文件)中显示相应的消息 EA停止。在这种情况下,EA交易必须重新初始化后才能运行。重新编译后或在打开其属性表并按下OK之后,可以重新初始化EA交易。

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端:



Now demonstrate the MQL4 side:

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

}

无论如何,请享受MQL4的狂野世界 -也可以单击并阅读有关MQL4问题的其他文章/ DLL集成和/或MQL4域中的信令/消息传递。 随意询问更多

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

传递参数
所有简单类型的参数均通过值传递,除非明确指出它们是通过引用传递的。传递字符串时,传递复制的字符串缓冲区的地址;如果字符串是通过引用传递的,则此字符串的缓冲区地址(不复制它)将传递给从DLL导入的函数。

包含动态数组,字符串,类,其他复杂结构的结构作为枚举对象的静态或动态数组,不能作为参数传递给导入的函数。

将数组传递给DLL时,始终传递数据缓冲区开头的地址(无论 AS_SERIES 标志)。 DLL中的函数对 AS_SERIES 标志一无所知,所传递的数组是长度不确定的静态数组;

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.

这篇关于如何通过引用将参数从MQL4传递给C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆