Table of Contents
MetaTrader + C++: DLL
MetaTrader can call functions exported from a Windows DLL. This is useful when an Expert Advisor or indicator needs code that is easier to maintain in C or C++, needs access to a native library, or needs better performance than a script-only implementation.
The important points are simple:
- Export plain C-style functions from the DLL.
- Use the correct calling convention expected by MetaTrader.
- Match MQL parameter types with the native function signature.
- Enable DLL imports in MetaTrader before running the script, indicator, or Expert Advisor.
C++ DLL Example
A minimal C++ DLL can expose functions like this:
// mt_example.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
extern "C" __declspec(dllexport) int __stdcall Add(int a, int b)
{
return a + b;
}
extern "C" __declspec(dllexport) double __stdcall Multiply(double a, double b)
{
return a * b;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID reserved)
{
return TRUE;
}
extern "C" prevents C++ name mangling, so MetaTrader can find the exported function by name. __declspec(dllexport) makes the function visible outside the DLL. __stdcall is commonly used for functions imported by MQL.
Build the DLL for the same architecture as the MetaTrader terminal. A 32-bit terminal needs a 32-bit DLL, and a 64-bit terminal needs a 64-bit DLL.
MQL Import Example
After compiling the DLL, place it where MetaTrader can load it, usually in the terminal’s MQL4/Libraries or MQL5/Libraries directory.
For MQL4:
#import "mt_example.dll"
int Add(int a, int b);
double Multiply(double a, double b);
#import
void OnStart()
{
int sum = Add(2, 3);
double product = Multiply(1.5, 4.0);
Print("sum = ", sum);
Print("product = ", product);
}
For MQL5, the same idea applies:
#import "mt_example.dll"
int Add(int a, int b);
double Multiply(double a, double b);
#import
void OnStart()
{
int sum = Add(2, 3);
double product = Multiply(1.5, 4.0);
Print("sum = ", sum);
Print("product = ", product);
}
Common Checks
If MetaTrader cannot call the DLL, check these items first:
- DLL imports are enabled in the terminal and in the script, indicator, or Expert Advisor settings.
- The DLL is in the correct
Librariesdirectory. - The DLL architecture matches the terminal architecture.
- Exported function names are not mangled. Use a tool such as
dumpbin /exports mt_example.dllor Dependency Walker to inspect the exports. - The MQL declaration exactly matches the C++ function signature.
Notes on Strings and Arrays
Primitive values such as int and double are the easiest to pass. Strings, arrays, and structures require more care because memory ownership and layout must match what MetaTrader expects.
When passing strings, prefer a small test function first and verify encoding and buffer size before using it in trading code. When passing arrays, pass both the pointer-like parameter and the length, and never assume the DLL can safely read beyond the supplied size.
Practical Rule
Keep the DLL interface small and stable. Let MQL handle trading logic and MetaTrader-specific state, while the C++ DLL handles isolated native work such as calculation, integration with another native library, or performance-sensitive processing.
