DLL works fine in script but not in EA

 

Hi

Apologies if this has been covered before - I have searched pretty thoroughly but did not find exactly what I was looking for. Thanks very much in advance!

1) I have a dll I wrote in c++ (based on this article, https://www.mql5.com/en/articles/18) which relies upon another dll file and a data file. 

2) I started by testing this with a script. It failed when the dll, dependent dll, and required data file, were in the “..\MQL5\Libraries” folder.

3) When I moved everything to “C:\Program Files\MetaTrader 5” it worked fine.

4) However when I tried this with a EA, I can compile and run the EA fine. However, when the EA calls the function in the dll I get the following error: “cannot find 'double MLEvaluatorDLL::MLRegressionFit(double&[],int)' in module 'MLEvaluatorDLL.dll'”

At the top of both the script and the EA is the following code. It is identical in each case.

#import "MQLEvaluatorDLL.dll"
double MLRegressionFit(double &arr[],int arr_size);
#import


The code for the dll (c++) is very simple:

// dllmain.cpp : Defines the en
pchtry point for the DLL application.
#include "pch.h"



BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

_DLLAPI double __stdcall MLRegressionFit(double* arr, const int arr_size)
{
    std::vector<std::string> ourHeaders = { };  //headers are hardcoded but omitted from this forum post for brevity
    std::vector<float> ourData;
    for (int i = 0; i < arr_size; i++)
    {
        ourData.push_back(arr[i]);
    }

    ModelCalcerWrapper calcer("catboost_model.cbm");
    double output = calcer.Calc(ourData, ourHeaders);
    return output;
}


pch.h:

#ifndef PCH_H
#define PCH_H

// add headers that you want to pre-compile here
#include "framework.h"

#endif //PCH_H


framework.h:

#pragma once

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>
#include <vector>
#include <string>
#include "wrapped_calcer.h"

#define _DLLAPI extern "C" __declspec(dllexport)


"wrapped_calcer.h" is the external dependency (a catboost implementation).

 
johnboy85:


4) However when I tried this with a EA, I can compile and run the EA fine. However, when the EA calls the function in the dll I get the following error: “cannot find 'double MLEvaluatorDLL::MLRegressionFit(double&[],int)' in module 'MLEvaluatorDLL.dll'”

At the top of both the script and the EA is the following code. It is identical in each case.


You did not show neither of your MQL5 programs (nor the script, nor the EA), so we can only speculate what's wrong.

First of all, the libraries (ex5 or dlls) are linked "statically" (so to speak) to ex5 host programs, that is they are loaded (and checked for all used imports/exports) when the main program is loaded, so the situation as you describe it "run the EA fine... when the EA calls the function in the dll I get the following error" is impossible, or you did not specify some important facts.

Second, the error “cannot find 'double MLEvaluatorDLL::MLRegressionFit(double&[],int)' in module 'MLEvaluatorDLL.dll'” references MLEvaluatorDLL library, whereas you're importing MQLEvaluatorDLL.