Does Pass By Reference work with ex4 libraries ?

 

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

 
Using MT4 Build 509
 

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 :(

 

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)?

 
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.
 
WHRoeder:
Either form is identical. I prefer with the data type.

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

 

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.

 

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.

 

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...

 
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.
 

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 ..

Reason: