MT5 small dll example (Delphi 2009)
library Project1;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Math,
Windows, // it needed for the MessageBox function
Dialogs, // it needed for the ShowMessage function from the Dialogs unit
SysUtils, // the UnixDateDelta constant from there
DateUtils, // the IncSecon and DateTimeToUnix functions are used
Graphics,
ap in 'src\ap.pas';
{$R *.res}
//----------------------------------------------------------+
//
//----------------------------------------------------------+
var Buffer: PWideChar;
const BUFFER_SIZE = 255;
const COLS = 2;
type
DoubleArray2D = array[0..2047, 0..COLS-1] of double;
//----------------------------------------------------------+
//
//----------------------------------------------------------+
function GetArray2DItem(var rates: DoubleArray2D;
const lines: Integer;
const l: Integer;
const c: Integer
):Double; stdcall;
var arr: TReal2DArray;
i,j: Integer;
begin
SetLength(arr,lines,COLS);
// copying data to two-dimensional array
for i:= 0 to lines - 1 do
for j:= 0 to COLS - 1 do
begin
arr[i,j]:= rates[i,j];
end;
Result:=arr[l,c];
end;
//----------------------------------------------------------+
//
//----------------------------------------------------------+
exports
GetArray2DItem;
//----------------------------------------------------------+
//
//----------------------------------------------------------+
procedure DLLEntryPoint(dwReason: DWord); // events handler
begin
case dwReason of
DLL_PROCESS_ATTACH: // DLL attached to the process;
// allocate memory
Buffer:=AllocMem(BUFFER_SIZE);
DLL_PROCESS_DETACH: // DLL detached from the process;
// release memory
FreeMem(Buffer);
end;
end;
//----------------------------------------------------------+
//
//----------------------------------------------------------+
begin
DllProc := @DLLEntryPoint; //Assign event handler
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
//+------------------------------------------------------------------+
//| LR_Channel.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#define COLS 2;
#import "Project1.dll"
double GetArray2DItem( double &rates[][2],
const int lines,
const int l,
const int c
);
#import
double arr[][2]; //data array for processing in the ALGLIB format
//+------------------------------------------------------------------+
int OnInit()
//+------------------------------------------------------------------+
{
return(0);
}
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
//+------------------------------------------------------------------+
{
double item1,item2;
int len=75; //number of points for calculation
ArrayResize(arr,len);
// copying of history to a two-dimensional array
int j=0;
for(int i=rates_total-1; i>=rates_total-len; i--)
{
arr[j][0] = j;
arr[j][1] = close[i];
j++;
}
item1 = GetArray2DItem(arr,len,0,0);
item2 = GetArray2DItem(arr,len,0,1);
Print(item1,":",item2);
item1 = GetArray2DItem(arr,len,1,0);
item2 = GetArray2DItem(arr,len,1,1);
Print(item1,":",item2);
item1 = GetArray2DItem(arr,len,2,0);
item2 = GetArray2DItem(arr,len,2,1);
Print(item1,":",item2);
return(len);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
//+------------------------------------------------------------------+
{
}
//+------------------------------------------------------------------+