Using classes defined in a .dll

 

Hi everybody,

I was evaluating which is the best choice among MQL4 and MQL5, I'm a software developer so I really appreciate the OOP introduced in MQL5.
I have a C++ .dll which exposes some useful classes, my question is: can I directly use the classes defined in an external .dll from MQL5. I think this should be quite natural (because of the c++ like nature of MQL5) but I'm pretty sure it's not possible.
What do you suggest? A wrapper .dll is an ugly and uncomfortable way, plus you loose the OOP benefits.

Any suggestion is more than welcome


Thanks in advance

Marco 

 

Hi All,

    I have the same question. Can somebody from Metaquotes clarify?

 

Regards

 Vivek 

 

Hi!

Same question. Almost five years and nobody can help?

Thanks.

 
imsp:

Hi!

Same question. Almost five years and nobody can help?

Thanks.


Why don't you try it and report back?
 
honest_knave:

Why don't you try it and report back?


Can you give directions on how to import a dll class from mql5? I will give a try gladly

 
imsp:


Can you give directions on how to import a dll class from mql5? I will give a try gladly


Alas, no I can't. It is not something I've ever needed to explore (and I guess that is why the question remains unanswered - not a very common requirement!)

Good luck if you do try

 
imsp:


Can you give directions on how to import a dll class from mql5? I will give a try gladly

Reading the documentation before asking questions is generally a good idea.

It shows you exactly how to do it. You clearly didn't.....

https://www.mql5.com/en/docs/basis/preprosessor/import

There are examples of importing .dll files on that page.

Documentation on MQL5: Language Basics / Preprocessor / Importing Functions (#import)
Documentation on MQL5: Language Basics / Preprocessor / Importing Functions (#import)
  • www.mql5.com
Language Basics / Preprocessor / Importing Functions (#import) - Reference on algorithmic/automated trading language for MetaTrader 5
 
Jack Thomas:

Reading the documentation before asking questions is generally a good idea.

It shows you exactly how to do it. You clearly didn't.....

https://www.mql5.com/en/docs/basis/preprosessor/import

There are examples of importing .dll files on that page.


I may have misunderstood the question; I read it as not a simple import of a function but an import of a class.

 

It's not possible to use classes directly from a DLL or any compiled library.

This article https://www.mql5.com/en/articles/362 shows an interesting approach to use classes in a library.

Promote Your Development Projects Using EX5 Libraries
Promote Your Development Projects Using EX5 Libraries
  • 2012.02.20
  • o_o
  • www.mql5.com
Hiding of the implementation details of classes/functions in an .ex5 file will enable you to share your know-how algorithms with other developers, set up common projects and promote them in the Web. And while the MetaQuotes team spares no effort to bring about the possibility of direct inheritance of ex5 library classes, we are going to implement it right now.
 
Can you post the export table of your DLL? If the exported class is just a set of mangled functions, then you could probably create a mqh-wrapper class.
 

In these cases you don't export the class but auxiliary functions as Stanislav Korotky said, Lets say you have a class in your DLL like so:

  class AwesomeIndicator {
    double tellMeThePriceThatIShouldBuy() {
      return 42;
    }
  }

Now imagine that you need to create that class so that it can do some logic and for you and you get your magic price to buy, all you have to do is the reverse of what C++ is doing, C++ classes are a "syntax sugar" (they are more complex, but for the sake of simplicity, lets forget edge cases), for example:

  AwesomeIndicator* pointerToAwesomeIndicator = new AwesomeIndicator();
  
  // When you call a method.
  pointerToAwesomeIndicator->tellMeThePriceThatIShouldBuy();
  
  // C++ is actually doing something like this, it just allocates the memory for the class members and uses the pointer to reference that memory..
  tellMeThePriceThatIShouldBuy(pointerToAwesomeIndicator, ...otherParameters);

With that in mind, you'll need in your DLL a function to create/delete the object and additional functions for each method in your class that you want to expose:

  /// Pointers are just either Int32 or Int64 depending on the OS/processor.
  AwesomeIndicator* createAwesomeIndicator() {
    return new AwesomeIndicator();
  }
  
  /// To delete the class and release the memory you need to call this function.
  double deleteAwesomeIndicator(AwesomeIndicator* targetToDelete) {
    delete targetToDelete;
  }
  
  // Call the method inside the class.
  double tellMeThePriceThatIShouldBuy(AwesomeIndicator* targetIndicatorCreated) {
    return targetIndicatorCreate->tellMeThePriceThatIShouldBuy();
  }

Basically you will have a bunch of functions where you'll have to pass the pointer to the object you created. Lots of APIs (DirectX, OpenGL, etc...) works like this, you'll have no issues finding examples online.

Reason: