I worked up a simple sample C# DLL that is properly exported for use with Metatrader. I made use of the template that can be downloaded from C# Project Template for Unmanaged Exports.
C# Code for "testUMD.dll" below using R. Giesecke's template:
Code:
using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace testUnmanagedDLL
{
class Test
{
[DllExport("AddInteger", CallingConvention = CallingConvention.StdCall)]
public static int AddInteger(int Value1, int Value2) {
MessageBox.Show("Add Integers: " + Value1.ToString() + " " + Value2.ToString());
return (Value1 + Value2);
}
[DllExport("AddDouble", CallingConvention = CallingConvention.StdCall)]
public static double AddDouble(double Value1, double Value2) {
MessageBox.Show("AddDouble: " + Value1.ToString() + " " + Value2.ToString());
double Value3 = Value1 + Value2;
return (Value3);
}
[DllExport("AddDoubleString", CallingConvention = CallingConvention.StdCall)]
public static string AddDoubleString(double Value1, double Value2) {
MessageBox.Show("AddDoubleString: " + Value1.ToString() + " " + Value2.ToString());
double Value3 = Value1 + Value2;
return (Value3.ToString() );
}
[DllExport("returnString", CallingConvention = CallingConvention.StdCall)]
public static string returnString(string Input) {
MessageBox.Show("Received: " + Input);
return ("SEND to MT4");
}
// many thanks to anonymous for the code sample below!
[DllExport("ReturnDouble2", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static double ReturnDouble2() {
return 4.5;
}
}
MT4 Script 'testDLL' code below: File testUMD.dll added to experts/libraries folder
Code:
#import "testUMD.dll"
int AddInteger(int Value1, int Value2);
double AddDouble(double Value1, double Value2);
string AddDoubleString(double Value1, double Value2);
string returnString(string Input);
double ReturnDouble2();
#import
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
Print("AddInteger: " + AddInteger(250, 750));
double a = AddDouble(250,750);
Print("AddDouble: " + NormalizeDouble(a,4));
double d = StrToDouble(AddDoubleString(250, 750));
Print("AddDoubleString: " + NormalizeDouble(d,4));
string temp = "Send to DLL";
string recv = returnString(temp);
Print(recv);
double dd = ReturnDouble2();
Print("Returning Double from C#: " + NormalizeDouble(dd, 4));
//----
return(0);
}
Cursory Summary: Integer passing = success, double passing MT4 to C# = success, double passing from C# to MT4 = fail, string passing = success. I haven't experimented with arrays. Sample code written with VS 10 .NET 4.0.
If anyone knows how to successfully get doubles to pass from C# to MT4 without conversion to a string, please leave a comment!
------------
Edit: thanks for the feedback from anonymous! The code has been updated to reflect passing a double from C# to Metatrader.
By the way, you must use the 32 bit version of the DLL. The 64 bit version and the "Any CPU" version both failed with error #127 in Metatrader. This is understandable considering MT4 is a 32 bit program.
As per g3ro's suggestion I have made available for download both the testUnamanagedDLL Visual Studio solution and the testDLL MT4 script. Download testUnmanagedDLL VS solution