如何创建 C++ DLL(使用 DEV -C++)并在 MT4 脚本中使用(一步一步)

And*_*llo 5 c++ dll mt4

I am trying to create my own DLL with DEV-C++ IDE tool and trying to use it inside MT4 script. I tried to study the example file [MT4_HOME]\MQL4\Scripts\Examples\DLL\DLLSample.cpp available in any MT4 installation and I tried to follow the same logic with other script but without sucess. Below I am describing in great details steps i followed just to be clear. I would like to understand why following the described steps my own dll doesn't work.


System configuration

  1. Laptop with windows 10 ;
  2. Dev- cpp installed
  3. MT4 installed

Goals

  1. to write my own dll by using "dev–c++" IDE tool;
  2. compile the dll;
  3. use the dll into a simple script in mt4.

Steps

  1. First I create a folder on my desktop named mydll;
  2. I start dev-cpp IDE tool;
  3. File -> New -> Project;
  4. I select project type -> DLL
  5. I write project name: mydll
  6. I press OK button
  7. Then I choose the folder in which to save the project (the folder mydll created in desktop at step a) and press save
  8. At this point Dev –C++ showes me two file templates (dllmain.cpp, dll.h) but I ignore them and close them without saving them into the project. After closing them I also remove them from IDE tool (write click with mouse and click remove file for each of them)
  9. Now I rigth clik over devc++ project -> New File
  10. Now I paste into this file the source code of my own DLL. (the below code) Note: For people who are familiar with metatrader 4, please notice that this code is a fragment of the file [MT4_HOME]\MQL4\Scripts\Examples\DLL\DLLSample.cpp of standard MT4 installation

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
//---
#define MT4_EXPFUNC __declspec(dllexport)


BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  {
//---
   switch(ul_reason_for_call)
     {
      case DLL_PROCESS_ATTACH:
      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
      case DLL_PROCESS_DETACH:
         break;
     }
//---
   return(TRUE);
  }

MT4_EXPFUNC int __stdcall GetIntValue(const int ipar)
  {
   printf("GetIntValue takes %d\n",ipar);
   return(ipar);
  }
Run Code Online (Sandbox Code Playgroud)
  1. I save this file into my DEV-C++ project folder with name mydll.cpp
  2. Now, in DEV-C++ ide tool I press F9 button to compile this file.
  3. Observations: a. the compilation process completes succesfully without any errors and any warnings b. some files appears into the DEV-C++ project (mydll.dll, libmydll.def, libmydll.a, mydll.o, Makefile.win, mydll.layout).
  4. Now, I copy and paste the mydll.dll into [MT4_HOME]\MQL4\Libraries directory of MT4
  5. Now, I create an empty folder [MT4_HOME]\MQL4\Scripts\Examples\mydll
  6. I copy and paste the files mydll.cpp and libmydll.def into the [MT4_HOME]\MQL4\Scripts\Examples\mydll folder
  7. Finally, I create a new file named mydllTester.mq4 into the [MT4_HOME]\MQL4\Scripts\Examples\mydll folder. Below is the source code

#import "DLLTutorial.dll"
int    _Z11GetIntValuei(int);  
#import

void OnStart()
{
   int cnt=_Z11GetIntValuei(int(10)); 
   Comment(cnt);
}
Run Code Online (Sandbox Code Playgroud)
  1. I open the file mydllTester.mq4 with the MT4 code editor and I compile the file.
  2. 最终测试 作为最后一步,我进行了测试以检查这是否有效。我打开 Metatrader4,打开一个新图表,然后单击我的脚本。我的期望是数字 10 出现在图表的左上角,但它不起作用。你能帮我理解我犯错误的步骤是什么吗?非常感谢。致以最诚挚的问候

And*_*llo 4

最后我找到了问题的解决方案,现在我可以编写一个简单的 DLL 并从 MT4 成功调用它。步骤如下:

  1. 创建文件 mydll.cpp
  2. 写入文件mydll.cpp的内容

#include <stdlib.h> 

#ifdef __cplusplus 
extern "C" 
{ 
#endif 
__declspec(dllexport) int __stdcall DLLAdd(int i, int j) ; 
#ifdef __cplusplus 
} 
#endif 

__declspec(dllexport) int __stdcall DLLAdd(int i, int j) 
{ 
    return i+j; 
} 
Run Code Online (Sandbox Code Playgroud)
  1. 编译文件mydll.cpp(注意使用“TDM-GCC 32 bit-release”编译器进行编译,因为MT4是32位应用程序,它只能理解32位编译文件)。编译器将生成文件 mydll.dll , libmydll.def

  2. 将文件 mydll.dll 复制到 MT4 的 [MT4_HOME]\MQL4\Libraries 目录中

  3. 在 MT4 的 [MT4_HOME]\MQL4 目录中创建一个文件夹“test_script”(或者在 MT4 主文件夹中的任何位置)

  4. 将 libmydll.def 文件复制并粘贴到“test_script”文件夹中

  5. 在“test_script”文件夹中创建一个新脚本“mydlltester.mq4”

  6. 将“mydlltester.mq4”文件的内容写入如下


#property strict

#import "mydll.dll"
    int DLLAdd(int i, int j); 
#import

void OnStart()
{ 
   Comment(DLLAdd(2,3));
}
Run Code Online (Sandbox Code Playgroud)
  1. 用MT4编译器打开“mydlltester.mq4”文件并编译
  2. 最终测试:如果您现在在 mt4 中打开图表并运行脚本 mydlltester,您将看到图表左上角出现总和 5。干杯!!