Script-Global objects (singleton)

 

Hello,

I'd like to create a script global object, like a singleton, which is only created once but available across all ExpertAdvisors.

Say, new myClass : public CObject... 

Then use a pointer to the only instance of this object in all ExpertAdvisor.

I tried to accomplish this by a DLL. Unfortunately, unfortunately, MQL5 seems not to allow to pass pointers to DLL (where i could save this pointer).

Also, it seems not to be possible to cast a pointer into a long (or int), to save it as integer type.

 

Is this possible at all ? Anyone has an idea ?

Thank you 

 
chinaski:

Hello,

I'd like to create a script global object, like a singleton, which is only created once but available across all ExpertAdvisors.

Say, new myClass : public CObject... 

Then use a pointer to the only instance of this object in all ExpertAdvisor.

I tried to accomplish this by a DLL. Unfortunately, unfortunately, MQL5 seems not to allow to pass pointers to DLL (where i could save this pointer).

Also, it seems not to be possible to cast a pointer into a long (or int), to save it as integer type.

 

Is this possible at all ? Anyone has an idea ?

Thank you 

In my opinion you have to create this singleton in your DLL, then access it from each EA. I don't think there is a way to create it with mql5 and access it from mql5 in other thread. Not sure though.
 
angevoyageur:
In my opinion you have to create this singleton in your DLL, then access it from each EA. I don't think there is a way to create it with mql5 and access it from mql5 in other thread. Not sure though.

Thank you angevoyageur. 

It seems. My hope was to create this object in MQL5, save pointer in DLL, access this pointer in other Experts by getting it from DLL.

This way i could stay in MQL5.

GetPointer(instance) returns a CObject*. But it seems you can't pass this pointer to a DLL function. You get a compiler error. 

 
chinaski:

Thank you angevoyageur. 

It seems. My hope was to create this object in MQL5, save pointer in DLL, access this pointer in other Experts by getting it from DLL.

This way i could stay in MQL5.

GetPointer(instance) returns a CObject*. But it seems you can't pass this pointer to a DLL function. You get a compiler error. 

Yes, mql5 pointer aren't like C++ pointer, this is why I don't see a way to do what you want.

Again, unlike C++, the variable hobject from the example above is not a pointer to the memory, it is a descriptor of the object.

A less elegant solution, but staying inside mql5, is to use Global Variables of the terminal.
 

This article can give some ideas too.

Data Exchange between Indicators: It's Easy
Data Exchange between Indicators: It's Easy
  • 2010.01.15
  • Алексей
  • www.mql5.com
We want to create such an environment, which would provide access to data of indicators attached to a chart, and would have the following properties: absence of data copying; minimal modification of the code of available methods, if we need to use them; MQL code is preferable (of course, we have to use DLL, but we will use just a dozen of strings of C++ code). The article describes an easy method to develop a program environment for the MetaTrader terminal, that would provide means for accessing indicator buffers from other MQL programs.
Reason: