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

标签:
转载 |
分类: 交易系统 |
Create your own MetaTrader extension (dll) - Part 1
Hi folks!
Today we are going to go step by step creating our first MetaTrader extension (dll) in C++, let's don't waste the time and start by defining what's the MetaTrader extension (dll)?
What's the MetaTrader extension (dll)?
MQL4 language give you a limited things
to do with it and there are a lot of things you can not do in MQL4.
To get the full control of your Windows operating system (For
example, accessing the window registry or file handling APIs) you
have to choices:
1- To call/use the windows common dlls and import the functions you want, and this is an example:
#import "user32.dll"
int MessageBoxA(int hWnd, string lpText, string lpCaption, int
uType);
In the line above we used the #import keyword to import one of
the "user32.dll" function; MessageBoxA function. Then we can use
this function in our code like any normal function.
2- The second choice is creating our own
dll in c++ and use it in our code the same as the common windows
dlls. And that's what are we going to learn today.
The Tools you need:
I tried Visual Basic to create the MetaTrader dll but it failed, the truth is I didn't give it a lot of time and trials because the fact that the Visual Basic is not a real dll (activex) creator.
Note: If you find here any new terms that you don't understand them (like activex), if you can't ignore them you can ask me to explain them to you. But you don't need to know these terms to understand how to create your own dll.
So, the best choice was to use Visual c++, I used Microsoft Visual c++ 6 which you can download free express version of it from here: http://msdn.microsoft.com/vstudio/express/visualc/download/
Note:
Now let's create our first dll that says "hello world!"
Hello world!
1- The first step you have to run Visual C++ (Figure 1).
http://s14/bmiddle/5090a7d1x77d2e9db03ed&6904
Figure 1 - Visual C++
6
2- Now from File menu choose New and a dialog like that (Figure 2) will appear.
Figure 2 - New project
dialog
3- From this dialog choose "MFC AppWizard (dll)" and write a name for the project in the "Project Name" field (Figure 3) and click "OK".
http://s8/bmiddle/5090a7d1x77d2ec600047&6904
Figure 3 - MFC
dll
Note: You can choose "Win32 Dynamic-Link Library" instead of "MFC AppWizard (dll)" but this means you will not able to use "CString" string type, CString is a string type in MFC that makes the world easier.
4- Another dialog (Figure 4) will appear to you asking you for some option. leave the default options and click "Finish" button. And when the information dialog appear click "OK".
http://s13/bmiddle/5090a7d1x77d2ed51d4ac&6904
Figure 4 - Project
options
5- Congratulation! You have a new project
now "demo" project which you can start to write your dll in. Please
open the file "demo.cpp" and give it a look.
Now, we are going to write some code in this file:
#define MT4_EXPFUNC __declspec(dllexport)
you put this line after the lines:
#include "stdafx.h"
#include "demo.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
http://s13/bmiddle/5090a7d1x77d2ef6ada8c&6904
6- Add the end of the file "demo.cpp" and after this line:
CDemoApp theApp;
We are going to write our "Hello" function like that:
MT4_EXPFUNC void __stdcall
Hello(char* say)
{
MessageBox(NULL,say,"demo",NULL);
}
http://s14/bmiddle/5090a7d1x77d2f1043c8d&6904
7- Now we have the function "Hello" which
takes a string to say it and returns no thing (void).
But the world want to know our function and in c++ to make the
function availabe to the world we have to add the function name in
a file called the .def file.
Now open the demo.def file and add this line (the bold line) at the
end of the file:
; demo.def : Declares the module parameters for the DLL.
LIBRARY "demo"
DESCRIPTION 'demo Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
Hello
http://s1/bmiddle/5090a7d1x77d2f2268b60&6904
8- Compile the dll by pressing F7, if you are a lucky man like me you will not get any errors or warnings. Search for the demo.dll in Debug folder in the Demo project folder.
In the coming article we are going to test our dll and we are going to know more about the data type passed and received to and from the dll that we can use to write more advanced dlls.
Hope you find it a useful article!
Coders' Guru