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
Goals
Steps
#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)
#import "DLLTutorial.dll"
int _Z11GetIntValuei(int);
#import
void OnStart()
{
int cnt=_Z11GetIntValuei(int(10));
Comment(cnt);
}
Run Code Online (Sandbox Code Playgroud)
最后我找到了问题的解决方案,现在我可以编写一个简单的 DLL 并从 MT4 成功调用它。步骤如下:
#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)
编译文件mydll.cpp(注意使用“TDM-GCC 32 bit-release”编译器进行编译,因为MT4是32位应用程序,它只能理解32位编译文件)。编译器将生成文件 mydll.dll , libmydll.def
将文件 mydll.dll 复制到 MT4 的 [MT4_HOME]\MQL4\Libraries 目录中
在 MT4 的 [MT4_HOME]\MQL4 目录中创建一个文件夹“test_script”(或者在 MT4 主文件夹中的任何位置)
将 libmydll.def 文件复制并粘贴到“test_script”文件夹中
在“test_script”文件夹中创建一个新脚本“mydlltester.mq4”
将“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)
| 归档时间: |
|
| 查看次数: |
3298 次 |
| 最近记录: |