including .dll file in MQL5\ Libraries folder

 

Hello, I have a Dynamic Link library project in code blocks.  I want my EA to import functions from the codeblocks DLL project.  I was reading the documentation on Importing Function (#import), and tried to follow it closely.  It says that "MQL5 libraries are loaded from the terminal_dir\MQL5\Libraries folder."  I tried to copy and paste the .dll file into this directory, so that importing functions could be possible.   However, I could not find this directory anywhere!!  I searched in lots of places.  For example, searched in   C:\Program Files\MetaTrader 5\MQL5,  and all over my C drive.  My friend installed metatrader 5 and could not find it either,  does anyone else have this same problem??  I tried pasting the .dll file in terminal_installation_directory\MQL5\Include, but this didn't work.  The EA compiles fine, but when attached to a chart, it says the initialization failed and the expert is removed.   Does anyone have any ideas on how to make the  DLL work?   Here is my code

 DLL in codeblocks

main.h 

#ifndef __MAIN_H__

#define __MAIN_H__


#include <windows.h>


/*  To use this exported function of dll, include this header

 *  in your project.

 */


#ifdef BUILD_DLL

    #define DLL_EXPORT __declspec(dllexport)

#else

    #define DLL_EXPORT __declspec(dllimport)

#endif



#ifdef __cplusplus

extern "C"

{

#endif


void DLL_EXPORT SomeFunction(const LPCSTR sometext);

void DLL_EXPORT PrintInt(int someint);


#ifdef __cplusplus

}

#endif


#endif // __MAIN_H__ 

 

main.cpp 

 

#include "main.h"

#include <iostream>

using namespace std;


// a sample exported function

void DLL_EXPORT SomeFunction(const LPCSTR sometext)

{

    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);

}


void DLL_EXPORT PrintInt(int someint)

{

    cout << someint << "\n";

}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)

{

    switch (fdwReason)

    {

        case DLL_PROCESS_ATTACH:

            // attach to process

            // return FALSE to fail DLL load

            break;


        case DLL_PROCESS_DETACH:

            // detach from process

            break;


        case DLL_THREAD_ATTACH:

            // attach to thread

            break;


        case DLL_THREAD_DETACH:

            // detach from thread

            break;

    }

    return TRUE; // succesful

}

 

 

Calling_C++ EA 

 

//+------------------------------------------------------------------+

//|                                                  Calling_C++.mq5 |

//|                                                      Derek & Joe |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Derek & Joe"

#property link      "http://www.mql5.com"

#property version   "1.00"



 #import "mql5_DLL.dll"

   void PrintInt(int someint);

 #import


//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

  int cmon=3;

  PrintInt(cmon);

  

  

//---

   return(0);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

//---

   MqlRates rt[2];

//--- call the Evolved Trader application only for first ticks of new bar

   if(CopyRates(_Symbol,_Period,0,2,rt)!=2)

     {

      Print("CopyRates of ",_Symbol," failed, no history");

      return;

     }

   if(rt[1].tick_volume>1) return;

   

  

   

   

   

  }

//+------------------------------------------------------------------+ 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
oneilljo:

Hello, I have a Dynamic Link library project in code blocks.  I want my EA to import functions from the codeblocks DLL project.  I was reading the documentation on Importing Function (#import), and tried to follow it closely.  It says that "MQL5 libraries are loaded from the terminal_dir\MQL5\Libraries folder."  I tried to copy and paste the .dll file into this directory, so that importing functions could be possible.   However, I could not find this directory anywhere!!  I searched in lots of places.  For example, searched in   C:\Program Files\MetaTrader 5\MQL5,  and all over my C drive.  My friend installed metatrader 5 and could not find it either,  does anyone else have this same problem??  I tried pasting the .dll file in terminal_installation_directory\MQL5\Include, but this didn't work.  The EA compiles fine, but when attached to a chart, it says the initialization failed and the expert is removed.   Does anyone have any ideas on how to make the  DLL work?   Here is my code


1. When posting code in forum, please use SRC button,  it makes your code much easier to read for other user who wants to help you

 

2. I bet you also don't know where yor EA, CI, and script is located. Use TerminalInfoString's TERMINAL_DATA_PATH, and if you use Vista/7, you'll find it in C:\Users\<use name>\AppData\Roaming\MetaQuotes\Terminal\<unix code>

3. Read this also How to Exchange Data: A DLL for MQL5 in 10 minutes.


 
phi.nuts:

1. When posting code in forum, please use SRC button,  it makes your code much easier to read for other user who wants to help you

 

2. I bet you also don't know where yor EA, CI, and script is located. Use TerminalInfoString's TERMINAL_DATA_PATH, and if you use Vista/7, you'll find it in C:\Users\<use name>\AppData\Roaming\MetaQuotes\Terminal\<unix code>

3. Read this also How to Exchange Data: A DLL for MQL5 in 10 minutes.


Hello phi.nuts,  thanks for being such a good help.  I found the directory for MQL5/Libraries right where you said, and put the .dll file in there.  I read that article and tried to do exactly what it said, but in Codeblocks, not Visual C++.  When I tried to run the EA with the dll, I got an message in the experts log that said "DLL is not a 64 bit version."  So, I re-installed metatrader 5 , but using the 32 bit version.  Now when I try to run the EA with the DLL, I get two messages in the expert log.  "Cannot find fnCalculateSpeed in mql5_DLL.dll"  Is this because I am using codeblocks?  I really don't like visual C++.  Can you recommend a solution?

 

Here is the code.  Note that in codeblocks I changed the properties of the DLL project.  Under Build Targets,  I changed the output directory to    C:\Users\Joe\AppData\Roaming\MetaQuotes\Terminal\5D2A9C702A29311ED87B6AD8A346121B\MQL5\Libraries.  

I tried to follow all the steps in the article. 

 

 

 

//+------------------------------------------------------------------+
//|                                                  Calling_C++.mq5 |
//|                                                              Joe |
//|                                              www.uofgfinance.com |
//+------------------------------------------------------------------+
#property copyright "Joe"
#property link      "www.uofgfinance.com"
#property version   "1.00"

#import "mql5_DLL.dll"
int  fnCalculateSpeed(int &res1,double &res2);
#import

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//--- call
   int    speed=0;
   int    res_int=0;
   double res_double=0.0;

   speed=fnCalculateSpeed(res_int,res_double);
   Print("Time ",speed," msec, int: ",res_int," double: ",res_double);

  
  
  
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlRates rt[2];
//--- call the Evolved Trader application only for first ticks of new bar
   if(CopyRates(_Symbol,_Period,0,2,rt)!=2)
     {
      Print("CopyRates of ",_Symbol," failed, no history");
      return;
     }
   if(rt[1].tick_volume>1) return;
   
  
   
   
   
  }
//And main.cpp for the codeblocks dll project  :
#include "main.h"
#include <iostream>
using namespace std;



_DLLAPI int __stdcall fnCalculateSpeed(int &res1,double &res2)
  {
   int    res_int=0;
   double res_double=0.0;
   int    start=GetTickCount();
//--- simple math calculations
   for(int i=0;i<=10000000;i++)
     {
      res_int+=i*i;
      res_int++;
      res_double+=i*i;
      res_double++;
     }
//--- set calculation results
   res1=res_int;
   res2=res_double;
//--- return calculation time
   return(GetTickCount()-start);
  }

_DLLAPI void __stdcall heyC()
{
    cout << "Hey!";
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

//And now the main.h for the codeblocks dll project.  There are only two files in the project

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

#define _DLLAPI extern "C" __declspec(dllexport)

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

_DLLAPI int __stdcall fnCalculateSpeed(int &res1,double &res2);
_DLLAPI void __stdcall heyC();

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

 

 

 

 
oneilljo:

Hello phi.nuts,  thanks for being such a good help.  I found the directory for MQL5/Libraries right where you said, and put the .dll file in there.  I read that article and tried to do exactly what it said, but in Codeblocks, not Visual C++.  When I tried to run the EA with the dll, I got an message in the experts log that said "DLL is not a 64 bit version."  So, I re-installed metatrader 5 , but using the 32 bit version.  Now when I try to run the EA with the DLL, I get two messages in the expert log.  "Cannot find fnCalculateSpeed in mql5_DLL.dll"  Is this because I am using codeblocks?  I really don't like visual C++.  Can you recommend a solution?

 

Here is the code.  Note that in codeblocks I changed the properties of the DLL project.  Under Build Targets,  I changed the output directory to    C:\Users\Joe\AppData\Roaming\MetaQuotes\Terminal\5D2A9C702A29311ED87B6AD8A346121B\MQL5\Libraries.  

I tried to follow all the steps in the article. 

 

//And now the main.h for the codeblocks dll project.  There are only two files in the project

 

Yes :). There is different way of calling function inside dll that was created using Code::Block compare with the one created by VS 2010.

I study your code but hate to tell you this, I kind busy right now so maybe you will get answer later :(.

Sorry :(.

Hope other forumer who use Code::Block also willing to help. 

 
phi.nuts:

Yes :). There is different way of calling function inside dll that was created using Code::Block compare with the one created by VS 2010.

I study your code but hate to tell you this, I kind busy right now so maybe you will get answer later :(.

Sorry :(.

Hope other forumer who use Code::Block also willing to help. 

Thank you phi.nuts!  I look forward to your answer later when you are not so busy.  I can see why you are so busy when looking at all the stuff to moderate on the forum.  Hopefully someone else comes along in the meantime.

  Update:  I am looking into the  stdcall and cdecl calling convention.  I read the docs on "Call of Imported Functions."

 
oneilljo:

Thank you phi.nuts!  I look forward to your answer later when you are not so busy.  I can see why you are so busy when looking at all the stuff to moderate on the forum.  Hopefully someone else comes along in the meantime.

  Update:  I am looking into the  stdcall and cdecl calling convention.  I read the docs on "Call of Imported Functions."

Now I remember : Try to not to use __stdcall, just remove __stdcall from your C++ code, I think I had to coding like that with Code::Block for MT4 sometimes ago. See if that works with MT5 too.
 
phi.nuts:
Now I remember : Try to not to use __stdcall, just remove __stdcall from your C++ code, I think I had to coding like that with Code::Block for MT4 sometimes ago. See if that works with MT5 too.

Awesome, I arrived at the same conclusion and I will try it when I get home.  The WinAPI part of the codeblocks DLL project should take care of it.  (just a normal function call)   Thanks again phi.nuts!   

 
oneilljo:

Awesome, I arrived at the same conclusion and I will try it when I get home.  The WinAPI part of the codeblocks DLL project should take care of it.  (just a normal function call)   Thanks again phi.nuts!   

Did it work?
 
TripleHeinz:
Did it work?
It did!  I removed a bit more than just the __stdcall to make it work though.  Would you like the code for the codeblocks dll project and the code for the EA that uses the sample function?  
 
oneilljo:
It did!  I removed a bit more than just the __stdcall to make it work though.  Would you like the code for the codeblocks dll project and the code for the EA that uses the sample function?  

Sure, why not, thanks!

I create plain C DLLs using the D programming language whenever i need more advanced functionality for MetaTrader. It's much easier with D to comunicate with MQL5 programs but it will be a good resource to have your C++ headers just in case.

Cheers.

Home - D Programming Language
  • dlang.org
Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. The D programming language. Modern convenience. Modeling power. Native efficiency. Visit dconf.org for...
 
TripleHeinz:

Sure, why not, thanks!

I create plain C DLLs using the D programming language whenever i need more advanced functionality for MetaTrader. It's much easier with D to comunicate with MQL5 programs but it will be a good resource to have your C++ headers just in case.

Cheers.

Hey , so I sent instructions and code to my mate so he could use the dll too, I will copy and paste these same instruction/code here for your use if you need it.

 

 

1. Start codeblocks, hit    File -> New-> Project  and select "Dynamic link library" as the type of project.  Call it metatrader5_dll.

2.  The project is launched, you get main.h and main.cpp.  Replace the generated code with the code I emailed you.

3.  Under the projects tab on the left in codeblocks, right click the title of the project (metatrder5_dll) and got to properties.  Under build targets tab, change the objects output directory to your MQL5/Libraries folder.  This is the one that   was hard to find.  Mine was    "C:\Users\Joe\AppData\Roaming\MetaQuotes\Terminal\5D2A9C702A29311ED87B6AD8A346121B\MQL5\Libraries"        Yours will be something similar, you will have to search around for that filepath.

4.  Press "Build" , not "build and run."

5.  Uninstall metatrader 5, reinstall it as a 32 bit version, which isn't as easy as it sounds.  See this 49 second video for instructions.   https://www.youtube.com/watch?v=PfdPzzJAIAw

6.  Now, find the folder where the codeblocks DLL project is saved .  Click Bin -> Debug.  Now copy all 3 files into the mql5/Libraries folder.  (see above)

7.  Start metatrader 5, and make a new EA.  Delete the generated code and copy the EA code that I sent to you.  Make sure the settings in mt5 will allow for DLL's.

8.  Attach the EA to the chart. If it works, you should see a message in the experts log saying something like  "2013.01.09 22:06:56  Calling_C++ (EURUSD,H1) Time 1734291680 msec, int: 0 double: 0.0"

Now if you ever want to use some C++ stuff in your EA's you can change around this example function

 

//+------------------------------------------------------------------+
//|                                                  Calling_C++.mq5 |
//|                                                              Joe |
//|                                              www.uofgfinance.com |
//+------------------------------------------------------------------+
#property copyright "Joe and Derek"
#property link      "www.uofgfinance.com"
#property version   "1.00"

#import "metatrader5_dll.dll"
int  SomeFunction(int &res1,double &res2);
#import

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//--- call
   int    speed=0;
   int    res_int=0;
   double res_double=0.0;

   speed=SomeFunction(res_int,res_double);
   Print("Time ",speed," msec, int: ",res_int," double: ",res_double);
   

  
  
  
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlRates rt[2];
//--- call the Evolved Trader application only for first ticks of new bar
   if(CopyRates(_Symbol,_Period,0,2,rt)!=2)
     {
      Print("CopyRates of ",_Symbol," failed, no history");
      return;
     }
   if(rt[1].tick_volume>1) return;
   
  
   
   
   
  }

 

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT fnFillArray(int *arr,const int arr_size);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
MetaTrader 5 32 bit installation.avi
MetaTrader 5 32 bit installation.avi
  • www.youtube.com
How to install 32 bit version of MetaTrader 5 trading terminal on 64 bit operation system (www.wallstreet-forex.com)
Reason: