#import function used for classes - is it possible?

 

Dear Forum members,

I've worked on some programs in MQL4 for a year and now I decided to code some Library files as well.
I have some classes written in Include (.mqh) files and used already. They are working properly.

I wrote some functions in a Library file, compiled, and working so now I understand how it's usable. But I don't know, and haven't found any articles/instructions on writing classes in Library (.ex4) files. I tried to put the "export" command after/before the class declaration like if it was a simple function but it doesn't want to work.
Is it possible anyhow? Have anyone written classes in Library files and used like in an Include file? 

Thank you,
TommyRuzich

 

You have to use the #import command and specify which functions you want to use, also MQL requires the definition and use of #define MT4_EXPFUNC __declspec(dllexport) within your library.

I can't help much beyond that as i tried building my library in Visual Studio and ended up with loads of Visual Studio junk.

Do you mean a cpp library file? or an ex4 file? To include class file you have to put them in the "Include" sub directory and you can #include <asusual.mqh> they're not executable though.

Regards.

 

Hi Keelan,

Thanks for answer. I meant building an ex4 library.
It's working when I declare separate functions like this:

int MyCalculator(int value,int value2) export {

    return(value+value2);

}

 But I can't declare a class like this: 

class FirstClass export

{

public:bool Function1(int input1);

private: int Number;

};

 Neither like this:

class FirstClass {

public: bool Function1(int input1) export;

private: int Number;

}; 

bool FirstClass::Function1(int input1) export {

Number=input1; return(true);

}

 It gives: 'export' - method cannot be exported error. It's not .dll, only a simple MQL4 library file (.ex4). I tried to put "export" to several places but it gives 'export' - unexpected token error. The .mq4 file is attached.

Thanks.
TR 

Files:
library1.mq4  2 kb
 

You cannot move entire class into a library, the class must be implemented during compile time. But you may code part of its implementation into a library, using inheritance.

Promote Your Development Projects Using EX5 Libraries 

The pattern is for mql5, but works for MQL4 as well. 

 

Ovo,

Thank you very much, this is what I've been looking for. You have the solution. :)

TR 

Reason: