5/4/2014
C r eati ng M etatr ader DLLs wi th Lazar us / Fr ee Pascal @ For ex Factor y Login
Home
6:21am
Google Powered
Forums
Tr a de s
Ca l e n d a r
Plat Pl atfo form rm Tech Tech / Options Search This Thread Go to Post#
Ma r k e t
7
Subscribe
Br ok e r s
Reply to Thread
Creating Metatrader DLLs with Lazarus / Free Pascal Page 1
2
3
4
5
Go to Page# Bookmark Boo kmark Thread
Ne ws
First Unread First Post: Feb Post: Feb 6, 2010 5:54pm | Edited Dec 15, 2010 3:06am
Last Post
Quote
Post 1
7bit
Printable Version
In this thread i will try to collect all the small bits of information information that are not s o ob vious but need ed to create DLLs that play together w ith Metatader, a collection collection of minim minimal al and se lf contained code s nippets that illustrate how things a re done. The intended a udience are peop le already familiar familiar with programming, programming, this is no Pas cal tutorial, i will focus focus on ly on all the little little things that need to be know n for Metatrader spe cific cific development development projects. I will use Lazarus Lazarus,, the free IDE containing the the Free Pascal compil Pascal compiler er FPC, a n excellent and very advanced (Object)Pascal Compiler. I chose Laz arus/FPC be cause it is by far the ea siest a nd most efficient efficient tool for this job that is a vailable today, and it is free! Make sure you yo u are dow nloading nloading the 32 bit version, even if you are running Window s 64 bit on x64 hardware , Metatrader 32 bit version, is a 32 bit application and application and can o nly use 32 bit DLLs, the 64 bit version of Lazarus/FPC w ould by default produce 64 b it binaries which can not be used for our purpose. To create a ne w DLL project (if you you s tarted La zarus for the first time) click click on Project -> new project -> Library. Library. Give Give it a meaningful name name a nd sa ve it into a n empty folder. The project w ill initi initially ally consist of only tw o files. An .lpi file file containing all compiler compiler settings and and .lpr file file containing the source code, these two files are all you need to b ackup, pass on to your colleagues or manage in a source code repo sitory. Additional Additional units (if (if you decide to split up bigger projects) are saved a s individual .pas files. If you come from from a C background you w ill find it notable that the re is no need for make, .def .def or w hatever-files, hatever-files, everything that compiler compiler and linker linker nee d to know is written in the p ascal so urce itself, you you simply throw throw the main unit at the compiler compiler and it will figure figure o ut everything by itself: http://www.at.freepascal.org/advantage.html I will expand this first first Po sting over time with ex amples ample s of increasing in creasing complexity, complexity, whenever I have ne w e xamples. xamples.
1. Minimal example with strings I will start this t hread with a minimal minimal example of a w orking orking dll that will show you how to pass strings to your DLL and how to return strings to Metatrade r. Since Since we are us ing mode mode rn Object-Pascal instead of ancient and clumsy clumsy C o r C++ it is rea lly easy a nd intuitive: intuitive: This This is all that is needed o n the Pas cal side: Inserted Code
[b]library[/b] minimal; [color=DeepSkyBlue]{$mode objfpc}{$H+}[/color] FXCM Specs
Similar Threads Free trial- MYFX METATRADER plug in revolutionising trade management metatrader 32 replies Free EA C reating Software 2 replies IPC with Lazarus Lazarus and MT4 0 replies A Free Indicator for Metatrader Mt4 10 replies
[b]uses[/b] sysutils; [color=SeaGreen]// strings from and to Metatrader will always be passed // as PChar which is a pointer to a nullterminated C-string.[/color] [b]function[/b] foo(x: double; y: [color=Blue]PChar[/color]): [color=Blue]PChar[/color]; [b ][color=Red]stdcall[/color][/b]; [b]var[/b] s :[color=Black]ansistring[/color]; [color=SeaGreen]// reference counted and memory manag ed strings.[/color] [b]begin[/b] [color=SeaGreen] // our PChar will be copied into an ansistring automatically, // no need to worry about the ugly details of memory allocation.[/color] allocation.[/color] s := 'Hello ' + FloatToStr(x) + ' ' + y + '!'; [color=SeaGreen] // cast it back into a pointer. Metatrader will copy the // string from the pointer into it's own memory.[/color] memory.[/color] result := [color=Blue]PChar[/color](s); [b]end;[/b] [b]exports[/b] foo; [b] begin end.[/b]
and this is the corres ponding mql4: Inserted Code
http://www.for exfactor y.com/showthr ead.php?t= 219576
1/9
5/4/2014
Creating Metatrader DLLs with Lazarus / Free Pascal @ Forex Factory #import "minimal.dll" [color=SeaGreen]// declare the imported function exactly as it is exported by the dll[/c olor] [color=Blue]string[/color] foo(double x, [color=Blue]string[/color] y); #import int init(){ [color=Blue]string[/color] s = foo(42.3, "Worlds"); Print(s); [color=SeaGreen]// will print "Hello 42.3 Worlds!"[/color] } int start(){ }
The very first and most important thing you need to know a bout DLLs use d for MT4 is marked with red color: Metatrader ass umes the 'stdcall' calling convention w hich means MT4 will push the a rguments onto the stack from right to left and our function is res ponsible for cleaning the sta ck before returning. FPC's default calling convention would be 'register', also known as 'Borland fastcall', which would use processo r registers for the first three 32 Bit arguments. Using the wro ng calling convention wo uld immediately crash Metatra der w ith a segfault due to a mess ed up s tack. Thus w e must decorate all exported functions w ith the keyword stdcall. Another option for achieving the same goa l would be the compiler directive {$CALLING STDCALL} at the top o f each unit conta ining exported functions.
2. Passing doubles and integers by reference
Home
By reference means in Metatrader simply putting an a mpersand & behind the type identifier in the function declaration and the function can then write values to these arguments. This is a convenient w ay to return more than one value from a function. imported functionsCalendar this seems only poss ible with arrays. For some we ird reason w e cannot Forums Trades News Market Brokers Unfortunately with just pa ss s calar value s by refere nce to a DLL, MT4 wo uld just pu sh a b unch of ze ros o nto the sta ck which is quite us eles s. We ne ed to do this with the help of arrays. If we p ass an array by reference MT4 will pass a Po inter to the first array element which is ea sily handled in Pas cal either by a typed po inter to an array that can be dere ferenced or even more elegantly by declaring the arguments w ith the keyword var w hich is technically the same, only nicer: Inserted Code
[b]library[/b] testlib; [color=DeepSkyBlue]{$mode objfpc}{$H+}[/color] [b]type[/b] TDPair = array[0..1] of double; TIPair = array[0..1] of LongInt;[color=SeaGreen] // 32 bit int[/color] [color=SeaGreen]// function parameters declared as var will accept pointers.[/color] [b]procedure[/b] VarsByReference([color=Red][b]var[/b][/color] a: TDPair; [color=Red][b]var [/b][/color] b: TIPair); stdcall; [b]begin[/b] [color=SeaGreen]// now let's make some changes to the variables[/color] a[0] += a[1]; a[1] -= a[0]; b[0] += b[1]; b[1] -= b[0]; [b]end;[/b] [b]exports[/b] VarsByReference; [b]begin[/b] [b]end.[/b]
and in mql4: Inserted Code
Platform Tech / C reating Metatrader DLLs with Lazarus / Free Pascal
http://www.forexfactory.com/showthread.php?t=219576
Page 1
2
3
4
5
2/9