Download MetaTrader 5
To add comments, please log in or register
They are already making profit using the Market. You can join them!
ydrol
595
ydrol 2013.07.07 23:07 

I have a simple example that doesnt seem to work. A script calls a function that should change the argument value.

It works if the function is in the script , but does not work via import?

Script Code:

#import "libtest.ex4"
int refTestLib(int &inout);
#import

int refTest(int &inout)
{
    inout += 5;
    return (inout);
}

int start()
 {
   int i = 88;
   int j = refTestLib(i);
   Alert("lib test values are different i="+i+"j = "+j);
   
   j = refTest(i);
   Alert("local test values are same as expected i="+i+"j = "+j);

   return(0);
 }

Library Code (libtest.mq4):

#property library

int refTestLib(int &inout)
{
    inout += 5;
    return (inout);
}


 

Output is :

lib test values are different i=88j=93

local test values are same as expected i=93j=93

ydrol
595
ydrol 2013.07.07 23:09  
Using MT4 Build 509
ydrol
595
ydrol 2013.07.07 23:24  

I found a hack looking at how DLLs work for other languages. An array has to be used.

LIBRARY

int refTestLib(int& inout[])
{
    inout[0] += 5;
    return (inout[0]);

}

SCRIPT

#import "libtest.ex4"
int refTestLib(int& inout[]);
#import




int start()
 {
   int i[] = {88};
   int j = refTestLib(i);
   Alert("lib test values are same too! i="+i[0]+"j = "+j);
  
 

   return(0);
 }


Need to change a fair bit of code now :(

GreenMoney
379
GreenMoney 2013.07.08 17:18  

In the code you posted first, you put the ampersand (the '&') with the variable name.  It should go with the variable type--in your case, with int (for example, int&).  EDITED: See WHRoeder's comment below.

In the External functions definition section of the documentation, it states:

Pointers to variables can be passed to imported dll functions. Data of the string type are passed as a pointer to the corresponding memory block (one should keep in mind that internal representation of string data consists of two parts: the memory block length and the memory block pointer). If there is a need to pass data of the int or double type, then the one-dimensional array of the corresponding type should be passed by reference as a parameter.

So, given the above, there is a distinct possibility that you will have to use a one-dimensional array to pass by reference.

Also, if your refTestLib is actually changing the value of the argument, why do you need to declare refTestLib as an int and return an int?  Wouldn't it be better (at least better coding style--personal preference only) to declare reTestLib as void and not return any value?  Or better still, what about declaring refTestLib as a bool and return whether it successfully updated the argument (for example, true for successfully updated, false for not successfully updated)?

whroeder1
13644
whroeder1 2013.07.08 17:35  
Thirteen: In the code you posted first, you put the ampersand (the '&') with the variable name.  It should go with the variable type--in your case, with int (for example, int&).
Either form is identical. I prefer with the data type.
GreenMoney
379
GreenMoney 2013.07.08 18:01  
WHRoeder:
Either form is identical. I prefer with the data type.

Hmmm...didn't know that.  Thanks for the clarification. :)

ydrol
595
ydrol 2013.07.09 10:03  

Hi @Thirteen,

re coding style ,  refTestLib was just a test to demonstrate the 'feature', not my real code, that's why I returned the result both via a reference and using 'return' - just to highligh the difference using as little code as possible.


re external functions, thanks , I saw that,  and the dll example, I understand these issues when using dlls between different languages, I'm just surprised it also apples to mq4 modules in the same language.

coastarica
13
coastarica 2013.09.16 04:24  

Cool! It seems true that MT4 can't accept simple ref variable, only array can do such things, I spend hours to find answer for this question, you find a really working way.

ydrol
595
ydrol 2014.01.28 11:35  

I've just tested the above with version 578 and it now works as expected. That is scalar pass by reference is now supported correctly across library calls.

Although I guess with proper use of object properties there should be less need for this....

Now I just need to check something else and if all is well I can start migrating my code into libraries and then classes...

Ovo Cz
2954
Ovo Cz 2014.01.28 13:40  
ydrol:

I've just tested the above with version 578 and it now works as expected. That is scalar pass by reference is now supported correctly across library calls.

Although I guess with proper use of object properties there should be less need for this....

Now I just need to check something else and if all is well I can start migrating my code into libraries and then classes...



Do you mean mql4 libraries? From my point of view, there is no reason to use them with the new MQL4 any longer. Or perhaps is, but I did not reveal it yet.
ydrol
595
ydrol 2014.01.28 13:47  

Hi I'm looking up how mql4 relates classes and files (if at all) . My code is already split into several files, each of which could more or less become a class. I just need to understand how the file structure will change.

EDIT: Doh ... found New Class in MetaEditor ..

/
To add comments, please log in or register