How to create C++ DLL (with DEV -C++) and use in MT4 script (Step by step)
Asked Answered
P

1

6

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

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

  1. I open the file mydllTester.mq4 with the MT4 code editor and I compile the file.
  2. Final test As the final step, I make a test to check if this works. I open Metatrader4 , I open a new chart and I simply click over my script . My expectation is that the number 10 appears on the top left bottom of the chart buti t doesn’t work. Can you help me to understand which is the step I am making mistakes? Thank you very much, Best Regards
Porker answered 15/8, 2017 at 16:21 Comment(3)
Which errors do you get? What did you observe when you stepped through your code line by line with the debugger?Ratib
Unfortunatelly I am a beginner with C++ so I don't know how to debug the compiled DLL. However, when I run the final tester script in MT4 (step 19 above ) I get nothing: no errors, no output messages in MT4 chart . On the other hand, If I try to run the default script available in MT4 installation it perfectly works .Porker
Maybe some of these links are helpful.Ratib
P
4

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:

  1. Create file mydll.cpp
  2. Write the content of file 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; 
} 

  1. 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

  2. Copy the file mydll.dll into the [MT4_HOME]\MQL4\Libraries directory of MT4

  3. Create a folder "test_script" into the [MT4_HOME]\MQL4 directory of MT4 (or wherever you want inside the MT4 main folder)

  4. Copy and paste inside the "test_script" folder the libmydll.def file

  5. Create a new script "mydlltester.mq4" inside the "test_script" folder

  6. 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));
}

  1. Open the "mydlltester.mq4" file with the MT4 compiler and compile it
  2. Final test: if you now open a chart in mt4 and run the script mydlltester you will see the sum 5 appearing on the top left corner of the chart. Cheers!!
Porker answered 16/8, 2017 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.