I am creating a MQL4 script which imports a C++ DLL to access some functions. The imported C++ DLL is created by me and it invokes third party licensed C++ libraries to perform some mathematical calculations.
I am able to run the code with out any issues and I get the expected output but only for the first time. When I try to rerun the MQL script 2nd time onwards, I am not getting the output [No run time errors]. If I close the meta trader terminal and rerun the script, I get the output again. Can someone please help me fix this issue? Given below is my code:
int J_Test(double &Arr_1, double &Arr_2, int, double &Output_1, double &Output_2);
C++ DLL Code
#define MT4_EXPFUNC __declspec(dllexport)
MT4_EXPFUNC int __stdcall J_Test(double *Array_1, double *Array_2, const int rows, double *Out_1, double *Out_2)
// Create a 2D array out of 2 1D arrays
double ** Array2d = new double*[rows];
for (int cnt_1 = 0; cnt_1 < rows; ++cnt_1)
Array2d[cnt_1] = new double;
//Filling the 2D array created above with data from input arrays Array_1 and Array_2
for (int cnt_1 = 0; cnt_1 < rows; cnt_1++)
Array2d[cnt_1] = Array_1[cnt_1];
Array2d[cnt_1] = Array_2[cnt_1];
int v_Ret_Cd=func_Calc(Array2d, rows, 2, 3, 1.25, *Out_1, *Out_2);
//J_TEST calls below function
MT4_EXPFUNC int __stdcall func_Calc(double **i_Array, int i_Rows, int i_Cols, int i_Arg4, double i_Arg5, double &i_Out_1, double &i_Out_2)
int v_Ret_Cd = -1;
// Calling the 3rd party library function. This function does math operations on 2D array based on input arguments and returns output to the right most 2 arguments
v_Ret_Cd = SDK_JTEST(i_Array, i_Rows, i_Cols, i_Arg4, i_Arg5, &i_Out_1, &i_Out_2);
Thanks in advance.
[...] Can someone please help me fix this issue?
You need to put some logging inside your DLL to see how far it is getting. It sounds as though it must be dying inside SDK_JTEST(), in a way which isn't logged as an exception by MT4.
One possible reason is that your data could be said to have 2 rows and 20 columns, rather than vice versa. Are you certain that you are passing the data in the form which SDK_JTEST() expects? What happens if you do SDK_JTEST(..., i_Cols, i_Rows,...) instead of SDK_JTEST(..., i_Rows, i_Cols,...) ?
Thanks jjc for you reply. SDK_JTEST is getting data the way it wanted else it wouldn't return outputs to the i_Out_1 and i_Out_2 variables.
I converted the DLL to C++ console application and passed the 1D arrays from main() function to JH_Test(). It ran with out any issue no matter how many times I run. So atleast with in C++ code there is no issue I can say.
I will try to add logging with in the DLL and see what happens.