MQL4 Reference Language Basics Functions Description of External Functions

Description of External Functions

External functions defined in another module must be explicitly described. The description includes returned type, function name and series of input parameters with their types. The absence of such a description can lead to errors when compiling, building, or executing a program. When describing an external object, use the keyword #import indicating the module.

Examples:

#import "user32.dll"
  int     MessageBoxW(int hWnd ,string szText,string szCaption,int nType);
  int     SendMessageW(int hWnd,int Msg,int wParam,int lParam);
#import "lib.ex4"
  double  round(double value);
#import

With the help of import, it is easy to describe functions that are called from external DLL or compiled EX4 libraries. EX4 libraries are compiled ex4 files, which have the library property. Only function described with the export modifier can be imported from EX4 libraries.

Please keep in mind that DLL and EX4 libraries should have different names (regardless of the directories they are located in) if they are imported together. All imported functions have the scope resolution corresponding to the library's "file name".

Use of several functions of the same name from different execution contexts in a program may cause ambiguity. To avoid the ambiguity of function calls, always explicitly specify the function scope using the scope resolution operation.

Example:

#import "kernel32.dll"
   int GetLastError();
#import "lib.ex4"
   int GetLastError();
#import
 
class CFoo
  {
public:
   int            GetLastError() { return(12345); }
   void           func()
     {
      Print(GetLastError());           // call of the class method
      Print(::GetLastError());         // call of the MQL5 function
      Print(kernel32::GetLastError()); // call of the DLL library function from kernel32.dll
      Print(lib::GetLastError());      // call of the EX4 library function from lib.ex4
     }
  };
 
void OnStart()
  {
   CFoo foo;
   foo.func();
  }

See also

Overload, Virtual Functions, Polymorphism