MQL4 - automated forex trading   /  

Documentation

MQL4 Reference  Basics  Preprocessor  Importing of functions

 

Functions are imported from compiled MQL4 modules (*.ex4 files) and from operating system modules (*.dll files). The module name is specified in the #import directive. For compiler to be able to form the imported function call and pass parameters in a proper way, the full description of functions is needed. Functions descriptions follow the #import "module name" immediately. The new #import command (can be without parameters) completes the imported functions description block.

#import "file_name"
    func1 define;
    func2 define;
    ...
    funcN define;
#import

Imported functions must have their unique names. Functions having the same names cannot be imported simultaneously from different modules. Imported functions names may not coincide with those of built-in functions.

Since the imported functions are out of the module to be compiled, the compiler cannot check correctness of parameters passed. This is why, to avoid runtime errors, it is necessary to declare the compliance of types and order of parameters precisely. The parameters passed to imported functions (both from EX4 and from DLL modules) cannot have values by default.

Examples:

#import "user32.dll"
   int    MessageBoxA(int hWnd, string lpText, string lpCaption, int uType);
#import "stdlib.ex4" string ErrorDescription(int error_code); int RGB(int red_value, int green_value, int blue_value); bool CompareDoubles(double number1, double number2); string DoubleToStrMorePrecision(double number, int precision); string IntegerToHexString(int integer_number);
#import "ExpertSample.dll" int GetIntValue(int); double GetDoubleValue(double); string GetStringValue(string); double GetArrayItemValue(double arr[], int, int); bool SetArrayItemValue(double& arr[], int,int, double); double GetRatesItemValue(double rates[][6], int, int, int); int SortStringArray(string& arr[], int); int ProcessStringArray(string& arr[], int); #import

For importing of functions during execution of an mql4 program, the so-called late binding is used. This means that until the imported function has not been called, the corresponding module (ex4 or dll) will not be loaded.

It is not recommended to use the fully qualified name of loaded module appearing as Drive:\Directory\FileName.Ext. MQL4 libraries are loaded from the terminal_dir\experts\libraries folder. If the library has not been found, there will be an attempt to load a library from the terminal_dir\experts folder.