How to write a dll for MT4?

 

Hello everyone,

I do not know how to write a dll for mt4?
When I write a DLL in C# and export it for 32bit or 64bit it do not work.


C# Code:

using System;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;


namespace ClassLibrary1
{
    public class Class1
    {

        public static int add(int a, int b)
        {
            return a + b;
        }
    }
}

MT4 CODE: (at this one Expert-Advisor crashes -> It removes the EA from chart)

#property copyright "Copyright 2020, chris1engel"
#property link      " "
#property version   "1.00"
#property strict
#import "ClassLibrary1.dll"
int add(int a, int b);
#import


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert(add(1, 3));
   
//---
   return(INIT_SUCCEEDED);
  }

another code to try: (This one the EA doesn't crash, but it do nothing. Do not even anything, when I place an Alert signal before)

#property copyright "Copyright 2020, chris1engel"
#property link      " "
#property version   "1.00"
#property strict
#import "ClassLibrary1.dll"


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  Alert(Class1::add(2, 3));
   
//---
   return(INIT_SUCCEEDED);
  }


Can someone help me please?

 

Hi, I can only guess. I think it's because of managed code. There should be an option to build the Dll in native mode.

And did you check out the sample Dll project in MQL4 - Scripts - Examples - DLL folder?

 
lippmaje:

Hi, I can only guess. I think it's because of managed code. There should be an option to build the Dll in native mode.

And did you check out the sample Dll project in MQL4 - Scripts - Examples - DLL folder?

Hey Thank you for your answer.

I checked the code out. It is C++
I try to build a WinForms on MT4 with C#.

 
WinForms on MT4? I'd begin with something simple and try to build on that. Did you export your C# function, it doesn't seem so?
 
lippmaje:
WinForms on MT4? I'd begin with something simple and try to build on that. Did you export your C# function, it doesn't seem so?
Yes I export them. But it doesn't work and I do not know why.
 
Chris E.:
Yes I export them. But it doesn't work and I do not know why.
No idea. I suggest to change the title to 'How to write a C# dll...' so it points out the problem. :/ And it's the same for MT5.
 
lippmaje:
No idea. I suggest to change the title to 'How to write a C# dll...' so it points out the problem. :/ And it's the same for MT5.
[DllExport("addme", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.I4)]
        public static int addme([MarshalAs(UnmanagedType.I4)] int a, [MarshalAs(UnmanagedType.I4)] int b)
        {
            return a + b;
        }


Got the solution.

I am using "using RGiesecke.DllExport;"

 
Yeah, that's what I meant with "it doesn't seem so." Anyways you tackled it, nice.
Reason: