Easiest way to calla C# dll in MQL

 

Hi, I've been searching & reading a lot of articles of  dll development but no projects seems to be sound.Most of them no longer works.

I know how to call c++ dll in MQL but can't find a working/simple solution for calling C# dlls. 

Can any of you show  me a simplest example with just 1 imported function(ex. Add(x,y),Sub(x,y) etc.) , via C#? 
Please don't refer to another link. I think I've seen all those related threads already & they didn't work for me. Just a practical code example, please.

 
Tusher Ahmed:

Hi, I've been searching & reading a lot of articles of  dll development but no projects seems to be sound.Most of them no longer works.

I know how to call c++ dll in MQL but can't find a working/simple solution for calling C# dlls. 

Can any of you show  me a simplest example with just 1 imported function(ex. Add(x,y),Sub(x,y) etc.) , via C#? 
Please don't refer to another link. I think I've seen all those related threads already & they didn't work for me. Just a practical code example, please.


1)You know how to call( import ) dll written by c++ but you don't know dll by c#?(this is a rhetorical question) (2) the coding for calling functions from dll does not depend  on which language you choose to write that dll...so... 

 

Tusher Ahmed:

...

I know how to call c++ dll in MQL but can't find a working/simple solution for calling C# dlls. 

...

Don't do it. Learn C++. I know this doesn't help you but the combo C# and MQL will not make you a happy developer. It is possible but too cumbersome. With C/C++ everything is like kindergarden.

@Chunsheng Yang: You better read/check again what you wrote. Maybe you will understand...

 

Forum on trading, automated trading systems and testing trading strategies

C# expert advisors, don't waste your time

Matthew Colter, 2016.11.27 05:01

I've spent a lot of time looking around at different options for writing expert advisors in C# and I can tell you with certainty that all of the available solutions are terrible. Wasting your time attempting to write expert advisors in any language besides MQL is a terrible idea. I know, you want to use a modern programming language that lets you do routine things without rewriting common algorithms that were pretty well thought out over a decade ago, well too bad. You're stuck using this domain specific language, with all it's limitations and bugs, and the only solutions out there are basically wrappers that pass information back to MQL functions anyway. Skip the whole mess of debugging the language you choose, the wrapper layer, and then the MQL layer, just to get Linq, third party libraries that work, garbage collection, expert systems, etc. oh... and Try {} Catch {}, you don't need any of that.

Basically, there are two flavors of wrapper out there in any sort of useful state:

1. Create a server wrapping every MQL function in an expert advisor, script, or indicator, then have the C# expert advisor post messages that the MQL code reads and executes in a loop.

2. Create an unmanaged dll exporting every MQL function, call those unmanaged functions in managed code and deal with marshaling, disposing, and basically managing the unmanaged code... but hey, you're still going to need an expert advisor, script, or indicator that loads the unmanaged dll and initiates the whole process so... basically messages and loops that end up in MQL again.

You just can't escape the DSL here, so stop trying. There is no client API, no UI plugin system, no UI extension system. You can just create unmanaged C++ dll and consume them in your MQL expert advisors. Don't cry, that's just how it is, and the deeper you look the more you can see that it is completely intentional. You will write code, and you will be locked into this platform, end of story.

If you want a trading platform that allows you to code in C# and trade with reputable brokers, just google it. If you love Metatrader, learn it's language.


Exposing C# code to MQL5 using unmanaged exports
Exposing C# code to MQL5 using unmanaged exports
  • 2011.02.09
  • investeo
  • www.mql5.com
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#.
 
alphatrading:

Don't do it. Learn C++. I know this doesn't help you but the combo C# and MQL will not make you a happy developer. It is possible but too cumbersome. With C/C++ everything is like kindergarden.

@Chunsheng Yang: You better read/check again what you wrote. Maybe you will understand...

You are right.  Welcome to -world of "managed" code, no more GC involved. 

 

Here is a good example of C# dll in meta

https://www.forexfactory.com/showthread.php?t=676853

 

Using dll written in native codes for MQL is not without dealing with quirks (even with system dll); for example,  MQL officially does  not allow developers to pass pointers.  Either you need to find undocumented tricks to work around or wait for official solution (such as StringToCharArray() for you to pass around C-style "string" with MQL's array name, which is bona fide a POINTER to wchar array).

Now, using C# ( so-called managed code ) for dll requires extra "layer" since MT4/5 is written with native code--logically speaking, it is doomed to generate more quirks. With MQL's only "#import", it is not hard to figure out the obstacle involved for a person who devotes to use C#.  I believe programming is about using your brain, understanding logic from a top-down perspective---so as not to start off in wrong direction.

I focus more on native code (since I think it is better to allow professionals to handle GC). I did mistakenly assume there is an easy way to "encapsulate" Dll in C# just like all the other DLLs in native codes (otherwise, why bother?) In Wall Street or other similar professional hubs, C/C++ dominates serious (back-end) finance application delivery. Go figure.

Reason: