加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何通过引用从MQL4传递参数到C DLL

发布时间:2020-12-16 07:17:30 所属栏目:百科 来源:网络整理
导读:我试图在MQL4中编写一个简单的程序,它通过引用将变量传递给C DLL函数,并在MQL4中打印更新的变量值.以下是我的代码. DLL功能: void Test(int* X){ *X = 6;} MQL4代码 #import "Test.dll"void Test(int);#importvoid OnStart(){ int A; Test(A); Alert(A);}
我试图在MQL4中编写一个简单的程序,它通过引用将变量传递给C DLL函数,并在MQL4中打印更新的变量值.以下是我的代码.

DLL功能:

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,6 );

  // ---------------------------------------------------<POST>
     Print( " TEST::  final values are: A = ",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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读