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);
}
#import "DLLTutorial.dll"
int _Z11GetIntValuei(int);
#import
void OnStart()
{
int cnt=_Z11GetIntValuei(int(10));
Comment(cnt);
}
Finally I found a solution of my issue and now I am able to write a simple DLL and call it from MT4 with success. Below the steps:
#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;
}
Compile the file mydll.cpp (pay attention to compile using "TDM-GCC 32 bit-release" compiler because MT4 is 32 bit application and it only understand 32 bit compiled files). The compiler will produce files mydll.dll , libmydll.def
Copy the file mydll.dll into the [MT4_HOME]\MQL4\Libraries directory of MT4
Create a folder "test_script" into the [MT4_HOME]\MQL4 directory of MT4 (or wherever you want inside the MT4 main folder)
Copy and paste inside the "test_script" folder the libmydll.def file
Create a new script "mydlltester.mq4" inside the "test_script" folder
Write the content of "mydlltester.mq4" file as below
#property strict
#import "mydll.dll"
int DLLAdd(int i, int j);
#import
void OnStart()
{
Comment(DLLAdd(2,3));
}