Calling dll function that returns an array

 
Hi all!

I've created a dll in C with a function that returns an array.
How can I call this function from MQL4?

Here is a brief excerpt:

dll:

MT4_EXPFUNC double* __stdcall GetRegression(const double* y, int size, double tolerance)
{
double* result = new double[size];
...
return result;
}

mql4:

double r[] = GetLinearRegression(Open, Bars, 0.0); //ERROR: 'Bars' - Variable expected.


What can go wrong here? Maybe it is with the Open array and not the return value?
 
gergomeister:
I've created a dll in C with a function that returns an array.
How can I call this function from MQL4? [...]

I think you have two separate issues here. Firstly, I don't think MQL4 allows DLLs to return arrays. Instead, I think you will have to give the array to the DLL function as an in/out parameter, and have the function populate the array provided by MQL4. Secondly, I don't think that you can pass the array of Open[] prices directly to a DLL. I think you need to take a copy of it into an array of your own, and then pass that to the DLL - see https://www.mql5.com/en/forum/119040

 
I have still problems with arrays.
The dll is compiled fine. It gets the call. But the array passed is null in the dll.

I have no idea what can be wrong :s

MQL4:

#import "test.dll"
double SumArray(double arr[], int size);
#import

int start()
{
double prices[];
ArrayCopySeries(prices, MODE_OPEN, "EURUSD", 0); //This is fine, the array is copied
double r = SumArray(prices, Bars); //This call returns -1 because the prices array that is passed has null value!
}

DLL:

MT4_EXPFUNC double __stdcall SumArray(const double* arr, int size) {

double sum = 0;
if (arr==NULL) //the array is null though it is not null in the mql4 file
{
return -1.0;
}
...
return sum;
}
 
gergomeister:
I have no idea what can be wrong :s

Try passing the array by reference: double SumArray(double & arr[], int size);

 
I've been working with a DLL very recently, so DLL hell is fresh for me.

You actually have to create yet another array and copy the Series array to it.
The series array isn't an array we can use directly in DLL apparently.

Here's how I do it for an individual series array(the test should return -2):

//mql import

#import
double ArrTest(double prices4dll[],int pries4dllsize, double prices[], int pricessize);
#import


//MQL main

double prices[];
ArrayCopySeries(prices, MODE_OPEN, "EURUSD", 0);
double prices4dll[];
ArrayCopy(prices4dll,prices);

int prices4dllsize=ArraySize(prices4dll);
int pricessize=ArraySize(prices);

Print(ArrTest(prices4dll, prices4dllsize, prices, pricessize));

//CPP
MT4_EXPFUNC double __stdcall ArrTest(double prices4dll[],int prices4dllsize, double prices[],int pricessize)
{

if (prices4dll==NULL)
{
return -1;
}
if (prices==NULL)
{
return -2;
}
return(0);
}


That should do it.
Reason: