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.
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__
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.
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."
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.
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!
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!
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.
- dlang.org
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.
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__

- www.youtube.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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;
}
//+------------------------------------------------------------------+