NavigationUser loginWho's new
Who's onlineThere are currently 0 users and 57 guests online.
|
External functionsHi folks, Today we are going to talk about a very important development issue; The external functions in MQL4. We all know the normal functions in MQL4, if you don’t know what are the functions (or you forgot) I’ll repeat some my speech about the functions I told you in my MQL4 Functions lesson. What are the MQL4 functions anyway?The function is very like the sausage machine, you input the meat and spices in the machine from one side and it outputs the sausage. The meat and the spices are the function parameters and the sausage is the function output (return value). The machine itself is the function body. There’s only one difference between the functions and the sausage machine that some of functions will return nothing (nothing in MQL4 called void). Let’s take an example: double //type of sausage -return value type As you see above the function starts with the type of the returned value "double" followed by the function name which followed by parentheses. Inside the parentheses you put the meat and the spices, sorry, you put the parameters of the function. Then the function body starts and ends with braces. In our example the function body will produce the operation (a*b+c). The return keyword is responsible about returning the final result. You call the above function like this: double result = my_func(1.5 , 2 , 5.9); As you see in the above line of code, you first declared a double variable to hold the returning value of our function. Then you assigned to that variable the function calling. You called the function by writing the function name then passed to it between the brackets the proper parameters (three double values in our case). When MQL4 see the function name, it will take the provided parameters and goes to the function body to execute the code inside that body and return with the value and place it at the same point of the function calling. This was a summery explaining of the MQL4 function. What about the external functions in MQL4? 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. 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: //+------------------------------------------------------------------+ 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.
//+------------------------------------------------------------------+ In the code above to use an external function resides in a MQL4 library file we have taken two steps: Importing the external function: To be able to use the external function from your code you have to import it to your code. #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. #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 int result = MyExtFunction(10,20); 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: Download the file if you want and save it the scripts path then compile it:
//+------------------------------------------------------------------+ #property copyright "Copyright Coders Guru" int start() 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. The MessageBoxA function will pop up a message box like the one in figure 2. Figure 2 - Message box
|