Building Custom DLL using MinGW for MetaTrader

I use MinGW gcc compiler on a Ubuntu system, I faced bugging issues when loading DLL on MetaTrader without much clue what was wrong. After getting this solved, now I am now sharing steps required for everyone.

First of all you need to place your dll into Metatrader MQL5/Libraries folder and allow DLL import from Tools -> Options -> EA, thats easy.

Second no “.def” files are required as mentioned by many peoples, also no used of inspecting problem with dependency walker tool, which gives errors unncessarity but it works anyway.

I built a working DLL using both Visual C++ 2010 and MinGW32 compiler and they both worked

Typical eerors you will get on mt4 when launching a dll based EA/Indicator is
– cannot load DLL – 126
– unresolved import function call

First error means DLL was not found in required path, so you need to place it under MQL5/Libraries directory. then this error should go away. when compiling script further you will get second error which is nasty.

This has to do with exported function name mangling and not at all with dependent dlls as anyone might suspect, it works easier in Visual C++ but in MinGW it creates problem so to fix that you simply use

extern "C" {
//// your function here
}

Next remove any __stdcall declartion from function signature.

for example, keep only

extern "C" {
__declspec(dllexport) int TestDLL() {
return 1234;
}
}

Done!!!

Leave a Reply