close

  

  

External functions:


External functions are functions reside at external files not the source file you are writing right now. 
You can use these function by importing them from the external files to your code.
Those external files can be one of two kinds of files:

1- MQL4 executable files (ex4):

You can use the functions reside at an already compiled MQL4 program called library programs. The file must be located at the MetaTrader 4\experts\libraries path and must be compiled (.ex4) before importing the functions you want from them. We will know everything about using the functions reside at these types of MQL4 programs in this article. The creating of this type of program is the subject of another article.

2- Windows Dlls:

DLLs (Dynamic Link Libraries) are programs written in an advanced programming language (like Visual basic or C++) for Microsoft Windows operating system. They are very like the normal programs like the Microsoft Word or FireFox browser but they mostly have no user interface and the other important difference that they can loaded from more than one application.
They are mostly contains shared function which can be used with all the windows application.
Using DLLs from MQL4 give the language a very strong advantage and enables it to make a lot of things another applications can do.
For example you can use the windows winsock DLL to send and receive data through the internet. The options are very wide and even dangerous.
We will know everything about using the functions reside at windows DLLs in this article. The creating of these DLLs is out scope of our MQL4 development field.

Using external functions from a library file:

I’ve create a library file in the purpose of this article (The steps creating of this file is out or article’s scope). Download the file if you want and save it the libraries path then compile it:

  

//+------------------------------------------------------------------+
//| mylib.mq4 |
//| Copyright Coders Guru |
//|  http://www.metatrader.info |
//+------------------------------------------------------------------+
#property copyright "Copyright Coders Guru"
#property link "http://www.metatrader.info"
#property library
//+------------------------------------------------------------------+
//| My functions  |
//+------------------------------------------------------------------+
int MyExtFunction(int value,int value2)
   {
    return(value+value2);
   }
//+------------------------------------------------------------------+


In the above code we have created a normal function MyExtFunction which we will import it from another MQL4 program and use it. The MyExtFunction function takes two integers parameters and returns the value of those two numbers addition.

Let’s create a simple program which imports and uses the external function MyExtFunction.


/+------------------------------------------------------------------+
//| Exteranl functions 1 .mq4 |
//|  Copyright Coders Guru |
//| http://www.metatrader.info |
//+------------------------------------------------------------------+
#property copyright "Copyright Coders Guru"
#property link "http://www.metatrader.info"

#import "mylib.ex4"
  int MyExtFunction(int value,int value2);
#import

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
  {
//----
  int result = MyExtFunction(10,20);
  Alert ( result);
//----
   return(0);
}
//+------------------------------------------------------------------+


In the code above to use an external function resides in a MQL4 library file we have taken two steps:
1- Importing the external function from the library.
2- Using (calling) the function like any MQL4 function.

Importing the external function:

To be able to use the external function from your code you have to import it to your code.
Importing the function takes three lines of code as you see above:

#import "mylib.ex4"

This is the first import line, we use the #import keyword followed by the library name, the line doesn't end with semi-colon.

int MyExtFunction(int value,int value2);

Then in the second line you declare the function very like the normal functions by writing the type of returned value followed by the function name then the parameters of the function and their types. Without this declaration your program will not know anything about the external function. You can import as many function as you want, in this case you write every function in a separate line.
The declaration line must ends with semi-colon.

#import

To tell the compiler of MQL4 that you have finished your importing you have to write another line contains the #import keyword, but in this case without an external file.

Note: You use only one #import line to end all the opened import lines, for example:

#import "mylib.ex4"
  int MyExtFunction(int value,int value2);
#import "mylib2.ex4"
  double MyExtFunction2(double value3);
#import

Calling the external function:
If you have written right import block lines like the above description, you can call the external function very like calling any normal function in MQL4 program. In our program we called the MyExtFunction like this:

 

int result = MyExtFunction(10,20);
Alert ( result);

We passed the numbers 10 and 20 to the function and assigned the returned value to the integer variable result. The Alert function will pop up the result variable like figure 1.

Using external functions from a DLL file:
Importing an external function from a DLL file is the same as importing them from a library file. The only difference is the close import line, in the case of importing an external function from a DLL you have not to write the #import line.

 

Let's take an example:

//+------------------------------------------------------------------+
//| Exteranl functions 2.mq4 |
//| Copyright Coders Guru |
//| http://www.metatrader.info |
//+------------------------------------------------------------------+
#property copyright "Copyright Coders Guru"
#property link "http://www.metatrader.info" 

#import "user32.dll"
  int MessageBoxA(int hWnd,string lpText,string lpCaption,int uType);

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
  {
//----
  int result = MessageBoxA(NULL,"Helo world!","MQL4 Messagebox",0);
//----
   return(0);
}
//+------------------------------------------------------------------+


As you can notice in the program above the import lines are the same as the using external function from a library except the closing #import line.
In the above code we used a function of user32.dll the very important windows DLL, and called it in the code very like any normal function.

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Robin0320 的頭像
    Robin0320

    縫隙中求生存 Life in GAP

    Robin0320 發表在 痞客邦 留言(0) 人氣()