Download MetaTrader 5

Discussion of article "Exposing C# code to MQL5 using unmanaged exports"

To add comments, please log in or register
Register as a seller and start making profit!
MetaQuotes Software Corp.
Moderator
181394
MetaQuotes Software Corp. 2011.02.09 16:55 

New article Exposing C# code to MQL5 using unmanaged exports is published:

In this article I presented different methods of interaction between MQL5 code and managed C# code. I also provided several examples on how to marshal MQL5 structures against C# and how to invoke exported DLL functions in MQL5 scripts. I believe that the provided examples may serve as a basis for future research in writing DLLs in managed code. This article also open doors for MetaTrader to use many libraries that are already implemented in C#.

Figure 2. COM Interoperability model

Author: investeo

smartwart
12
smartwart 2011.02.13 05:52  
hello,
I think
in principle it's very useful to open up possible resources and find this article is very intersting. But I'm not very experienced programmer and therefore I have some question.

From my point of view unmanaged code gives me the possibility to develop platform undepended. But if somebody use Metatrader, he got a defined platform.

What is the benefit with unmanaged code in generally and in combination with MQL5 ?  - Please correct me if I have misunderstood something!


investeo
4098
investeo 2011.02.13 16:27  
smartwart:
hello,
I think
in principle it's very useful to open up possible resources and find this article is very intersting. But I'm not very experienced programmer and therefore I have some question.

From my point of view unmanaged code gives me the possibility to develop platform undepended. But if somebody use Metatrader, he got a defined platform.

What is the benefit with unmanaged code in generally and in combination with MQL5 ?  - Please correct me if I have misunderstood something!


Hi smartwart,

 

The benefit is that there is a great amount of already developed code in C# that you can use for trading purposes. You will read more in my next article.

 

cheerz,

Investeo

 

Karlis Balcers
6667
Karlis Balcers 2011.02.17 19:31  

This is best thing ever! I love C# and I was looking for any possibility to get it working! Best news of the year! I think this was last time when I wrote EA in MQL5. I will place all logic in libraries and will use one template EA.mq5 for all my EAs (just they will be linked with different libs).

 Thank you! 

BillyMadison
3
BillyMadison 2011.05.06 20:33  

*Huge* thanks for writing this article!  It's very well written and the multitude of examples are extremely helpful.

The big hurdle for me was figuring out the variable types and how to pass them back and forth. (especially arrays and strings)  It would be great to see some shared C# libraries for Metatrader.  Once I learn more and build some useful libraries, I'll see if I can contribute them.

You have saved me a lot of time instead of learning by trial and error.  Thanks for sharing your knowledge.

It's appreciated!  :-)


Tolstoj
7
Tolstoj 2011.06.28 13:41  

Hi

I have a problem that suddenly (?) my DLL function returns error message "Cannot find 'Add' in 'TestMe.dll'. This is a simple example, using your code:

using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace Testme
{
    class Test
    {

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

Using it in an .mql file:

#import "Testme.dll"
   int Add(int left,int right);  
#import

void OnStart()
  {
//---
   for(int i=0; i<3; i++)
     {
      Print(Add(i,666));     
     }
  }

 

--> Cannot find 'Add' in 'Testme.dll'

 

Is there a change in how to call DLLs that I have missed? 

 

Any opinion is greatly appreciated

Thomas 

lcinternal
3
lcinternal 2011.07.11 12:37  

Hi

 I also have a problem suddenly (it worked at first, but ...),  when I compiled the sample dll, I got

Assembling 'C:\Users\internal228\AppData\Local\Temp\tmpAFBE\UELTest.x86.il'  to DLL --> 'C:\Users\internal228\Documents\Visual Studio 2010\Projects\UnmanagedLibTest\UELTest\bin\Debug\x86\UELTest.dll'

Source file is UNICODE

***** FAILURE ***** 

UELTest

 

Anything I missed? 

Windows 7; VisualStudio 2010;

Any opinion is greatly appreciated.

Risan

Franco
374
Franco 2011.10.29 22:19  

**UPDATE**  Sorted it out thanks!


Hey there,

I followed the procedure entirely to write a dll in C# Express 2010.  The template did not work so I manually added the dll files in a new project and everything compiled nicely (maybe I did something wrong here).

I wrote two easy functions just to test if it works, and I'm testing out of Metatrader 4.  I get the following error:


2011.10.29 20:13:37    2000.01.03 00:00  Expert_NN_1_1 USDJPY,H1: cannot call function 'sum' from dll 'NN 1 DLL.dll' (error 127)


Any idea what might be the problem?  The DLL is loaded my MT4, I checked, but it does not want to call the function.  I noticed that this problem appears when trying to call a managed dll in MT4, but the procedure described in this article is supposed to solve the problem right?

Any help will be appreciated

Thanks!


GordonH
3
GordonH 2011.11.30 07:32  

Hi there,

Would just like to pass on my appreciation for such a well written article.

The site details for Robert Giesecke were invaluable.

I have learnt much. Not needing to write another C++ wrapper when I prefer VB.NET is a cool thing.

Many thanks. 

Cheers, 

Gordon 

parseval
3
parseval 2012.03.15 00:02  

Hi,

I have the same problem.... methods I decorate are not found in mt5!!

 

pls help!  (i'm using vs2010 and the template) 

battlmonstr
1
battlmonstr 2012.09.18 23:48  

That's a very interesting and useful article on general C++ and .NET interop.

The most simple strategy to implement is "2.4. C++/Cli wrapper class", because you can use .NET API in C++/cli without any hassle, so the only glue code is a small and straightforward class that holds "gcroot".

This approach was chosen as a ground for implementation of http://www.nquotes.net - a library which allows MQL programming in C#.

Although the first version only supports MQL4, but will definitely be ported to MQL5 in future versions.

NQuotes : MetaTrader (MT4) and C#/.NET bridge
  • www.nquotes.net
Debugging in Visual Studio IDE is possible. You can inspect values, place breakpoints and inspect execution step by step. Have a nicer experience than "Print" debugging in pure MQL. Compatible API NQuotes has an API that is compatible with MQL. It is quite easy to get started for MQL4 developers, while others can use the official...
12
To add comments, please log in or register