Aim
We would like to create a Win32 DLL by Visual Studio 2010. A script running on MT4 will then use that DLL as an example.
Learning Source
Steps
#define _DLLAPI extern "C" __declspec(dllexport)
// MQL5DLLSamples.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
//+------------------------------------------------------------------+
//| Passing and receving of simple variables |
//+------------------------------------------------------------------+
_DLLAPI int __stdcall fnCalculateSpeed(int &res1,double &res2)
{
int res_int=0;
double res_double=0.0;
int start=GetTickCount();
//--- simple math calculations
for(int i=0;i<=10000000;i++)
{
res_int+=i*i;
res_int++;
res_double+=(double)i*(double)i;
res_double++;
}
//--- set calculation results
res1=res_int;
res2=res_double;
//--- return calculation time
return(GetTickCount()-start);
}
//+------------------------------------------------------------------+
//| Filling the array with values |
//+------------------------------------------------------------------+
_DLLAPI void __stdcall fnFillArray(int *arr,const int arr_size)
{
//--- check input variables
if(arr==NULL || arr_size<1) return;
//--- fill array with values
for(int i=0;i<arr_size;i++) arr[i]=i;
}
//+------------------------------------------------------------------+
//| The substring replacement of the text string |
//| the string is passed as direct reference to the string content |
//+------------------------------------------------------------------+
_DLLAPI void fnReplaceString(wchar_t *text,wchar_t *from,wchar_t *to)
{
wchar_t *cp;
//--- parameters checking
if(text==NULL || from==NULL || to==NULL) return;
if(wcslen(from)!=wcslen(to)) return;
//--- search for substring
if((cp=wcsstr(text,from))==NULL) return;
//--- replace it
memcpy(cp,to,wcslen(to)*sizeof(wchar_t));
}
//+------------------------------------------------------------------+
//| Call for the crush |
//+------------------------------------------------------------------+
_DLLAPI void __stdcall fnCrashTest(int *arr)
{
//--- wait for receipt of a zero reference to call the exception
*arr=0;
}
//+------------------------------------------------------------------+
- Build the dll file and put that into MQL4/Libraries in the MT4 folder
- Prepare the mql4 script in MQL4/Scripts, DLLTest.mq4
//+------------------------------------------------------------------+
//| DLLTest.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//---
#import "MQL5DLLSamples.dll"
int fnCalculateSpeed(int &res1,double &res2);
void fnFillArray(int &arr[],int arr_size);
void fnReplaceString(string text,string from,string to);
void fnCrashTest(int arr);
#import
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
//--- calling the function for calculations
int speed=0;
int res_int=0;
double res_double=0.0;
speed=fnCalculateSpeed(res_int,res_double);
Print("Time ",speed," msec, int: ",res_int," double: ",res_double);
//--- call for the array filling
int arr[];
string result="Array: ";
ArrayResize(arr,10);
fnFillArray(arr,ArraySize(arr));
for(int i=0;i<ArraySize(arr);i++) result=result+IntegerToString(arr[i])+" ";
Print(result);
//--- modifying the string
string text="A quick brown fox jumps over the lazy dog";
fnReplaceString(text,"fox","cat");
Print("Replace: ",text);
//--- and finally call a crash
//--- (the execution environment will catch the exception and prevent the client terminal crush)
fnCrashTest(NULL);
Print("You won't see this text!");
//---
}
//+------------------------------------------------------------------+
- Build that script and test on MT4
- DONE