About Interfaces in Libraries - page 2

 
Yannick Deubel #:

Thanks @Alain Verleyen and @JeeyCi,

But I have the scenario that the library is only available as a binary. In my code example, the file EA.mq5 includes Interface.mqh (source code available) and the compiled version of  Library.mq5 ("Library.ex5").    

I see, that you want to use something just like this (in C++) - but

1) MQL documentation says only about  Exporting Functions, not classes... or import-link 

2) even in C++ - exporting class is considered to be the way to collect different problems for yourself

3d) in C++ you should remember that you're exporting a pointer! - in any case in C++ the pointer is certain memory, and in MQL it is just a handler, - and I doubt that you should deal MQL in exactly the same way as C++ - especially concerning pointers

4) you can try to ask MQL support - if it is possible to export Class from compiled binary .ex ... I think the main idea is - if MetaLang.exe compiler ALLOWS to import_to_mqClient/export_from_exLib pointers to classes?? (I have no evidences to believe in it) => therefore cannot loose the time to test your idea (though I really like it - to have the source code of library (with class, not only functions) in a binary_form) -- !! to continue with (the same as in C++) - different compilers used for compilation of different modules in 1 project (if any will once need to be recompiled) - can lead to incompatibility of these modules - therefore often source files are better then binaries (because you can compile all of them with ONE compiler_version!) - and in such a case you can need no export smth to somewhere - just doing it in a simpler way (I showed)

5) I use MQL4, and am not going to install MQL5 for you - but if I had it (I do not know whether mql5 have smth special concerning your problem) & if I knew the answer, of course, I could have answered more to you... but taking into consideration p.3 & p.4 of the current post - I really doubt that it is worth to loose time for it -- if not asking MQL-tech_support anyway (if at least they could confirm that it is possible technically in mql) - but I really have no facts that it is possible in MQL to export Class_pointer from binaries in order to use instances of this class anywhere outside the library...

therefore I prefer just to deal with Includes .mqh (as header stuff with all necessary classes & their implementations in .mqh-files)

(library-mq4-files to use for functions and also folder Library for dlls)

AND I SEE NO CHANCE TO DO WHAT YOU WOULD LIKE (at least in MQL4) - because I cannot even get Hello() in exactly code-case of yours - with error:

IMain * cMain();  - illegal returned type     // in EA.mq4


Exporting C++ classes from a DLL
  • eli.thegreenplace.net
Because of ABI incompatibilities between compilers and even different versions of the same compiler, exporting C++ classes from DLLs is a tricky business. Luckily, with some care it is possible to do this safely, by employing abstract interfaces. In this post I will show a code sample of a DLL and an application using it. The DLL exports a...
 

BTW, I found just such advice :

 Either a use a proxy class whose methods mediate interaction with non-public implementation,

or go into a fully distributed-processing model, where the private implementation is completely external to the MQL4 code-base and provide just interacting agents

1st am not sure that matters when talking about ex-binaries...

2nd way I consider however to be the only applicable - distributed-processing model

What is the correct way to define MQL4 "#import of static class methods"?
What is the correct way to define MQL4 "#import of static class methods"?
  • 2015.04.06
  • Joel Box Joel Box 411 5 5 silver badges 12 12 bronze badges
  • stackoverflow.com
What I'm trying to achieve is define classes (using ) in separate files and use the methods from those classes in the main code. Essentially importing static class member functions.
 
JeeyCi #:
   

AND I SEE NO CHANCE TO DO WHAT YOU WOULD LIKE (at least in MQL4) -

sorry for misleading info - something happened with my test-case that day... example from the beginning of the topic works! (in my MQL4)

& concerning your questions

Yannick Deubel:

Just a quick question:

... will GetSecret() be hidden from EA.mq5, since it is not defined inside Interface.mqh?

Also, will EA.mq5 ever get access to globalVar? Or can I safely declare global variables inside a library?

1) yes - because your EA don't see your lib.ex5, just your interface -- so GetSecret is safely hidden

2) as well as (1) globalVar is hidden - you exported only IMain * cMain(), nothing else was exported

 

👍🏻

Reason: