- Macro substitution (#define)
- Program Properties (#property)
- Including Files (#include)
- Importing Functions (#import)
- Conditional Compilation (#ifdef, #ifndef, #else, #endif)
Importing Function (#import)
Functions are imported from compiled MQL5 modules (*.ex5 files) and from operating system modules (*.dll files). The module name is specified in the #import directive. For compiler to be able to correctly form the imported function call and organize proper transmission parameters, the full description of functions is needed. Function descriptions immediately follow the #import "module name" directive. New command #import (can be without parameters) completes the block of imported function descriptions.
Imported functions can have any names. Functions having the same names but from different modules can be imported at the same time. Imported functions can have names that coincide with the names of built-in functions. Operation of scope resolution defines which of the functions should be called.
The order of searching for a file specified after the #import keyword is described in Call of Imported Functions.
Since the imported functions are outside the compiled module, the compiler can not verify the validity of passed parameters. Therefore, to avoid run-time errors, one must accurately describe the composition and order of parameters passed to imported functions. Parameters passed to imported functions (both from EX5, and from the DLL-module) can have default values.
The following can't be used for parameters in imported functions:
Classes, string arrays or complex objects that contain strings and/or dynamic arrays of any types cannot be passed as a parameter to functions imported from DLL.
To import functions during execution of a mql5 program, early binding is used. This means that the library is loaded during the loading of a program using its ex5 program.
It's not recommended to use a fully qualified name of the loadable module of type Drive:\Directory\FileName.Ext. MQL5 libraries are loaded from the terminal_dir\MQL5\Libraries folder.
If the imported function has different call versions for 32- and 64-bit Windows versions, both of them should be imported, and the right function version should be called explicitly using the _IsX64 variable.
Importing functions from .NET libraries
To work with .NET library functions, simply import DLL itself without defining specific functions. MetaEditor automatically imports all functions it is possible to work with:
- Simple structures (POD, plain old data) — structures that contain only simple data types.
- Public static functions having parameters, in which only simple types and POD structures or their arrays are used
To call functions from the library, simply import it:
The C# code of the Inc function of the TestClass looks as follows:
As a result of execution, the script returns the value of 42.