[转载]【转帖】MetaTrader 4 (MT4) DLL编程(II)

标签:
转载 |
分类: 交易系统 |
【转帖】MetaTrader 4 (MT4) DLL编程(II) |
Create your own MetaTrader extension (dll) - Part 2
Hi folks!
In the previous part of this article we created step-by-step our first MetaTrader extension (dll) in C++ and in this article we are going to give it a test.
Also we are going to study in this article and the coming article some of the advanced topics briefly (like how to use arrays and structure in your dll).
Hope you are ready for this hard journey which deserves the effort.
Let's test our demo.dll
We are going to write some code to test our demo.dll. These are the steps we have to take before saying "Hello World!"
1- We have the demo.dll in the debug folder in your Visual C++ projects folder. Copy the demo.dll to the library folder ("MetaTrader 4expertslibraries").
2- Open MetaEditor to create the include
file which will hold the declaration of our "Hello" function. This
is the code of demo.mqh
#import "demo.dll"
void Hello(string say);
#import
Note how we have imported the dll and how we have declared the "Hello" function with the same parameters (string say) and return value (void) as the function in the dll.
This file will be saved at the Include folder (MetaTrader 4expertsinclude).
3- Now, let's create the script to test the demo.dll. We are going to name it "Hello.mq4" and it must be saved at the Scripts folder (MetaTrader 4expertsscripts).
#include <demo.mqh>
int start()
{
Hello ("Hello World!");
return(0);
}
Note how we have started the code by including the demo.mqh file to make it a part of our code.
4- Compile the script (F5) and load it now (double click it in the Navigator window in the Terminal). What did you get? a nice dialog like the one showed in figure 1, if not please check everything or email me!
http://s12/bmiddle/5090a7d1x77d34184462b&6904
Note: You have to enable the option "Allow DLL imports" in MetaTrader before using any code that import external functions (from the common windows dlls or you custom dlls like our demo.dll) that's by going to Tools -> Options -> Expert Advisors tab then checking "Allow DLL imports" (Figure 2).
http://s14/bmiddle/5090a7d1x77d31ea0bded&6904
Advanced Sample (ExpertSample)!
Our demo.dll was a simple one yet it was a completed dll. There's an advanced sample that shipped with MetaTrader program which include advanced programming topics. We are going to study it and thank MetaQuotes for its ideal sample.
You will find the sample of the dll in
"MetaTrader 4EXPERTSSAMPLES";
the dll (ExpertSample.dll) in the folder "MetaTrader
4EXPERTSSAMPLESExpertSamp
You have to open the source code of the dll (To open the source code double click ExpertSample.dsw file) and give the code a look. You will find it more advanced and to some degree a complex one compared to the demo.dll, but don't be afraid we are going to know everything in the next article.