How To MT4 Call C# DLL Function

 
Hello everyone~
I want to use MT4 to call the DLL developed by C#
But i have a problem
I organize my development process below
Hope everyone can help me solve this problem

1.
I am using Visual Studio 2017 (My windows operating system is Windows 7 64-bit)
I create a new project, the project type is Visual C# Windows Forms APP

The project name is TestDLL



2.
I right click on the right project manager> Properties
Change the output type to "Category Library"
3.
I am from the NuGet package manager
Install the package "UnmanagedExports" first
Installed into my project TestDLL
4.
I am in Build> Configuration Manager
Go modified to use x86 to build the solution
5.
I added a new category to my project, named TestDLL
6.

My code in TestDLL.cs is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace TestDLL
{
    class TestDLL
    {
        [DllExport("Add", CallingConvention = CallingConvention.StdCall)]
        public static int Add(int left, int right)
        {
            return left + right;
        }

        [DllExport("AddDouble", CallingConvention = CallingConvention.StdCall)]
        public static double AddDouble(double left, double right)
        {
            return left + right;
        }
    }
}


7.
Rebuild the entire program

8.
I put the file in ..\TestDLL\TestDLL\bin\x86\Debug\TestDLL.dll in the path
Copy to the MT4 folder, the path is ..\MQL4\Libraries

9.
Installed Microsoft Build Tools 2015
https://www.microsoft.com/en-us/download/details.aspx?id=48159

Chinese(Traditional)


10.
I created an MQL4 file in MT4, named TestDLL.mq4

Then enter the code below

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict

#import "TestDLL.dll"
   int Add(int left,int right);
   double AddDouble(double left,double right);
#import

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print("Add Int=== " + Add(1,666));
   Print("Add Double=== " + AddDouble(666.5,1));
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+

11.
When I executed the file TestDLL.mq4 on MT4

But the following error message appears

And the value calculated by the Add function is also wrong, it should be 667 instead of 102238280




Which step am I wrong?

Thank you everyone! ~

PS.I uploaded the files I made using the visual studio 2017 project with MQL4

Files:
TestDLL.mq4  2 kb
TestDLL.zip  930 kb
 
zboo:
Hello everyone~
I want to use MT4 to call the DLL developed by C#
But i have a problem
I organize my development process below
Hope everyone can help me solve this problem

1.
I am using Visual Studio 2017
I create a new project, the project type is Visual C# Windows Forms APP

The project name is TestDLL



2.
I right click on the right project manager> Properties
Change the output type to "Category Library"



3.
I am from the NuGet package manager
Install the package "UnmanagedExports" first
Installed into my project TestDLL



4.
I am in Build> Configuration Manager
Go modified to use x86 to build the solution



5.
I added a new category to my project, named TestDLL




6.

My code in TestDLL.cs is as follows:


7.
Rebuild the entire program

8.
I put the file in ..\TestDLL\TestDLL\bin\x86\Debug\TestDLL.dll in the path
Copy to the MT4 folder, the path is ..\MQL4\Libraries

9.
Installed Microsoft Build Tools 2015
https://www.microsoft.com/en-us/download/details.aspx?id=48159

Chinese(Traditional)


10.
I created an MQL4 file in MT4, named TestDLL.mq4

Then enter the code below


11.
When I executed the file TestDLL.mq4 on MT4

But the following error message appears

And the value calculated by the Add function is also wrong, it should be 667 instead of 102238280




Which step am I wrong?
Thank you everyone! ~

There is a 32-bit requirement for the DLL for MT4. 64 bit does not support.


The MT4 and MT5 VS2017 screenshots are enclosed.

https://www.mql5.com/en/forum/293517#comment_9811172

MT4 DLL Not Support
MT4 DLL Not Support
  • 2018.12.04
  • www.mql5.com
MT4 also developed by its own company does not work why the DLL. Script under DLLSampleTester.mq4...
Files:
MT4.JPG  98 kb
MT5.JPG  63 kb
 
Mehmet Bastem:

There is a 32-bit requirement for the DLL for MT4. 64 bit does not support.


The MT4 and MT5 VS2017 screenshots are enclosed.

https://www.mql5.com/en/forum/293517#comment_9811172

Hi Mehmet Bastem~


Thank you for your reply~


Regarding the part you mentioned that MT4 only supports 32-bit

I changed it to x86 to build the dll file in the 4th step:


4.

I am in Build> Configuration Manager

Go modified to use x86 to build the solution


 
zboo:

Hi Mehmet Bastem~


Thank you for your reply~


Regarding the part you mentioned that MT4 only supports 32-bit

I changed it to x86 to build the dll file in the 4th step:


4.

I am in Build> Configuration Manager

Go modified to use x86 to build the solution


I still can’t connect at the moment, can anyone help me, thank you~~~

 

I wonder the problem, I could not fully understand. It works smoothly.

1-Please uninstall NUget packages in VS2017 and reinstall.

2- I tried again in the attachment, there is no problem.

3- Required pictures are attached.

DLL link http://www.haskayayazilim.net/Testme.dll

//+------------------------------------------------------------------+
//|                                                   Libdeneme1.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
#include <stdlib.mqh>
#include <WinUser32.mqh>


#import "testme.dll" 

int  Sub( int Ab1,int Ab2);
#import


int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
    int  num1=4;int num2=7;string result="";
    result=Sub(num1,num2);
    Print("result:",result," input num1:",num1,". num2:",num2);
    ExpertRemove();
  }
//+------------------------------------------------------------------+
Files:
NUget.JPG  75 kb
aaa.JPG  281 kb
bbb.JPG  317 kb
 
Mehmet Bastem:

I wonder the problem, I could not fully understand. It works smoothly.

1-Please uninstall NUget packages in VS2017 and reinstall.

2- I tried again in the attachment, there is no problem.

3- Required pictures are attached.

DLL link http://www.haskayayazilim.net/Testme.dll

Hi Mehmet Bastem~


Because I have just come into contact with functional development in this area, many of the questions are relatively basic. Thank you for your patience and careful reply~


I downloaded the DLL file you provided, and it works on my MT4 by the way


I have installed the two packages you mentioned into my project:

UnmanagedExports.Repack

UnmanagedExports.Repack.Upgrade



But I also encountered the same error message:



Or can you provide me with the project file you made?

Thanks for your assistance~


PS。When I finish building the project, do I only need to copy the TestDLL.dll file under the path ..\bin\x86\Debug\TestDLL.dll to the Libraries folder of MT4?

 

Ok. 

please uninstall the NUget packages and reinstall them. Do not use an upgrade.

I sent my own DLL project. In yesterday I posted limdeneme1.mq4 experti to use this DLL. It runs smoothly. I ran a test.

http://www.haskayayazilim.net/Testme32Bit.rar

please chance your expert code this.


#import "TestDLL.dll"
   int Add(int left,int right);
   double AddDouble(double xleft,double xright);
#import
DD function works, AddDouble does not work. in your project. Correct the input parameter names of the AddDouble function. Left input value is defined as int in ADD function, right below the variable defined as int in AddDouble function is defined as double. The DLL is not working, it gives the message that the AddDouble function could not be found due to an error parameter.
 
Mehmet Bastem:

Ok. 

please uninstall the NUget packages and reinstall them. Do not use an upgrade.

I sent my own DLL project. In yesterday I posted limdeneme1.mq4 experti to use this DLL. It runs smoothly. I ran a test.

http://www.haskayayazilim.net/Testme32Bit.rar

please chance your expert code this.

DD function works, AddDouble does not work. in your project. Correct the input parameter names of the AddDouble function. Left input value is defined as int in ADD function, right below the variable defined as int in AddDouble function is defined as double. The DLL is not working, it gives the message that the AddDouble function could not be found due to an error parameter.

Hi Mehmet Bastem~


Thank you for providing the project for me to study. Thank you very much for your kind assistance.


I downloaded the Testme32Bit.rar file and unzipped it.


After decompression is complete, open the project directly and remake the project

But the following error message appeared

How can I solve it?

(It seems that the path cannot be found?)

Thank you~



The "DllExportAppDomainIsolatedTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

The "DllExportAppDomainIsolatedTask" task could not be instantiated from "C:\Users\Afu-Work\Desktop\Testme32Bit\packages\UnmanagedExports.Repack.1.0.4\build\..\tasks\RGiesecke.DllExport.MSBuild.dll". Could not load file or assembly 'RGiesecke.DllExport.MSBuild, Version=1.2.7.38851, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)


PS.

I think it should refer to the problem on the path of RGiesecke.DllExport.Metadata

I tried to remove it, but I don’t know how to add it back

How did you join the RGiesecke.DllExport.Metadata reference


 

Please delete old information from NuGet. then reinstall it. The error seen is NUget packet error. The picture is attached.

Files:
nuget1.JPG  46 kb
 
Mehmet Bastem:

Please delete old information from NuGet. then reinstall it. The error seen is NUget packet error. The picture is attached.

Hi Mehmet Bastem~


Thank you for your assistance, let me go one step further~

I followed the steps you provided, when the reconstruction plan is being carried out on the current project side

There are no more error messages


My steps are as follows:


1. From the NuGet package manager of Visual Studio 2017, remove the following two packages

UnmanagedExports.Repack

UnmanagedExports.Repack.Upgrade


2. After saving the project, restart Visual Studio 2017


3. From the NuGet package manager of Visual Studio 2017, reinstall the two packages above


4. After the reconstruction plan, there is no error message at this time


5. Copy the ..\Testme\bin\x86\Debug\Testme.dll file to the Libraries folder of MT4



But I encountered a new problem. When calling the DLL on the MT4 platform, the following error message appeared:



I will attach my DLL file and MT4 MQL4 file

Thanks again for your assistance

Thank you! ~


PS. I have seen many functions under the installed items of NuGet package manager that can be upgraded. Will I need to upgrade each package here?

(I have not upgraded at the moment)


Files:
TestDLL.mq4  2 kb
Testme.zip  2 kb
 

No Problems.

testdll

Reason: