Create DLL , simplest form example – Other – 7 July 2023

Hi! 

First things first you need visual studio for this . 

Install it , start it up.

  1. Select the programming language
  2. Select the operating system
  3. Select Library
  4. Select Dynamic Link library , it will appear 

Click Next


give it a name 

Click Create 

wait for the slow pc to create the project


This is the first thing you may see , the explorer could be on the right in your setup .

It opens up with the dllMain.cpp in view :

Now , correct me if you are more experienced but ,whatever .cpp file (because we chose c++) you create inside the project could be accessible

in the DLL .

For this one let’s pack everything in one file since we s*ck at programming .  😊


So let’s create a new cpp file called “mySimpleDLL.cpp” , you can name it whatever you want don’t worry.

  1. In the solutions explorerTab right click on the name of the project
  2. Go to menu item “add”
  3. Go to menu item “New item”
  4. From the popup select “C++ file” , if you cant see it click “detailed view” bottom left
  5. Name your file
  6. Hit “add”


 Add the following on top of your new cpp file :

  1. Name it name + _EXPORTS 
  2. Name it name +_API 

Is that important (the naming convention) ? I don’t know , someone more experienced can explain.

here is the code form :

#pragma once

#include "pch.h" 
#include <utility>
#include <limits.h>

#ifdef MYSIMPLEDLL_EXPORTS
#define MYSIMPLEDLL_API __declspec(dllexport)
#else
#define MYSIMPLEDLL_API __declspec(dllimport)
#endif


Excellent , now let’s expose a function !

  1. extern “C” 
  2. name of api as defined
  3. return type 
  4. name of function 
  5. function parameters

in code , simple a+b in the return :

extern "C" MYSIMPLEDLL_API int hard_math(int a, int b) {
        return(a+b);
}

Awesome , but does it work ? 


On the compile tab on the header :

  1. Click on “Debug” (if it says release skip this step)
  2. Select “Release” 

wait for your fast pc to prepare


next to it 

  1. select x86 if the DLL is for MT4 
  2. select x64 if the DLL is for MT5


  1. click “Build” on the menu on top
  2. click “Build Solution”

If it compiles succesfully you will see this in the console 


Cool now , let’s find the file (the dll)

  1. under solution explorer right click the name of the project
  2. select “open folder in file explorer”


the file explorer will send you to a folder looking like this :


Go one level up , you need to be in the folder marked with the pink marker

  1. Navigate in the “Release” folder
  2. Your dll file is there for x86 (32bit)


Copy it and move it to your expert advisor folder to test it .

For the first test create a folder in experts called mySimpleDLL and place the mySimpleDLL.dll dll in that folder .

create a test1.mq4 file in the editor , it should look like this 

In the test1.mq4 we add this to the header to import the function from the dll : 

#import "mySimpleDLL.dll" 
int hard_math(int a,int b);
#import

let’s see if it does not work.

this is the source code :

#property version   "1.00"
#property strict
#import "mySimpleDLL.dll" 
int hard_math(int a,int b);
#import
int OnInit()
  {
  int result=hard_math(3,5);
  Print("DLL says "+IntegerToString(result));
  return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
  }

Compile it and when attaching to the chart make sure this is checked in the EA :

Also :

  1. Go to Tools
  2. Go to options
  3. Go to expert advisors
  4. Make sure the allow dll imports is checked

That’s it , time to test it .

it does . Awesome

Source link

Comments are closed.