.dll Matlab Engine

 

Hi

I have a problem loading a .dll I've written to connect MetaTrader to Matlab. Error is 126 "cannot load library". I've been trying to solve this since a week now and tried quite a lot:

Reference is: https://www.mql5.com/en/articles/1567

I'm using Visual C++ 2010.

For learning reasons I've written a very simple .dll that worked without any problems:

// dllmain.cpp : Definiert den Einstiegspunkt für die DLL-Anwendung.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"


#define MT4_EXPFUNC __declspec(dllexport)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

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;
}

MT4_EXPFUNC int _stdcall PlusZwei(int zahl1, int zahl2)
{
int res = 0;
res = zahl1+zahl2;
return(res);
}



This code I modified step by step. I also compiled a .def-file within the project. And I added the path to the .lib-files needed for Matlab Engine within Visual C++. The last code working is this:

//Update 1 ->2: add #include "engine.h"
//Update 2 ->3: delete #define MT4_EXPFUNC __declspec(dllexport)
// add extern "C" __declspec(dllexport) int __stdcall PlusZwei(int zahl1, int zahl2);
// add int _stdcall PlusZwei(int zahl1, int zahl2)
//Update 3 ->4: add #include "engine.h"


// dllmain.cpp : Definiert den Einstiegspunkt für die DLL-Anwendung.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stdafx.h"
#include "engine.h"

extern "C" __declspec(dllexport) int __stdcall PlusZwei(int zahl1, int zahl2);
//extern "C" __declspec(dllexport) bool __stdcall StartVirtualEngineMatlab(void);
//extern "C" __declspec(dllexport) bool __stdcall StoppVirtualEngineMatlab(void);



//#define MT4_EXPFUNC __declspec(dllexport)

#define BUFSIZE 256
Engine *pEng;
mxArray *Summand1 = NULL, *Summand2 = NULL, *mxSumme = NULL;
//int Output;


//#define MT4_EXPFUNC __declspec(dllexport)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

BOOL WINAPI DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
//pEng = NULL;
//StartVirtualEngineMatlab();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
//StoppVirtualEngineMatlab();
break;
}
return TRUE;
}


/*
bool __stdcall StartVirtualEngineMatlab()
{//Ãîòîâíîñòü: 100%. Ñîçäàíèå âèðò/ìàøèíû

//** Èíèöèàëèçàöèÿ âèðò/ìàøèíû

if ((pEng = engOpen(NULL)) == NULL)
{
MessageBoxA(NULL, (LPSTR)"Can't start MATLAB engine!",
(LPSTR) "MatLab Engine: ERROR!", MB_OK|MB_ICONSTOP);
return FALSE; //Âîçíèêëè ïðîáëåìû çàïóñêà âèðò/ìàøèíû
};

//
return TRUE;
}

//---------------------------------------------------------------------------

bool __stdcall StoppVirtualEngineMatlab()
{//Ãîòîâíîñòü: 100%. Óíè÷òîæåíèå âèðò/ìàøèíû

//** Äåèíèöèàëèçàöèÿ âèðò/ìàøèíû

engClose(pEng);

//
return TRUE;
}




int __stdcall PlusZwei(int zahl1, int zahl2)
{

Summand1 = mxCreateDoubleMatrix(1, 1, mxREAL);
Summand2 = mxCreateDoubleMatrix(1, 1, mxREAL);
mxSumme = mxCreateDoubleMatrix(1, 1, mxREAL);

memcpy((char *)mxGetPr(Summand1), (char *)zahl1, 1);
engPutVariable(pEng, "a", Summand1);

memcpy((char *)mxGetPr(Summand2), (char *)zahl2, 1);
engPutVariable(pEng, "b", Summand2);

engPutVariable(pEng, "Summe", mxSumme);

engEvalString(pEng, "Summe = a + b;");

mxSumme = engGetVariable(pEng, "Summe");

memcpy((char *)Output, (char *)mxGetPr(mxSumme), 1);

//int res = 0;
//res = zahl1+zahl2;

return(1);
}
*/

int _stdcall PlusZwei(int zahl1, int zahl2)
{
int res = 0;
res = zahl1+zahl2;
return(res);
}



The problems begin when I try to start Matlab Engine in the next step as seen in the code above in comments. I don't use Matlab to calculate anything - just start and stop it in BOOL WINAPI DllMain() in the switch. If I do so I get error 126! Also note that in the working code above definition Engine *pEng; does not lead to an error. So I think engine.h must be working somehow???

So how can I solve this? I suppose engine.h is not working correctly? But I included it in the project and compiled it within the project.


Would be very glad if someone has an idea to this matter!


Thanks in advance!

Max

 
try a standalone .exe that does the exact same and see if it can load all needed parts of matlab and start the engine. (all required DLLs must be able to load without errors or your mt4 dll itself would refuse to load)
 

Thanks! Good input. Now I know a little bit more:

I get error:

main.obj : error LNK2019: unresolved external symbol '_engOpen' referenced in function ""bool __stdcall StartVirtualEngineMatlab(void)" (?StartVirtualEngineMatlab@@YG_NXZ)"

Same for "_engClose"

And:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol '_main' referenced in function "___tmainCRTStartup"

 

is matlab installed and working and the header files belong to the correct version of matlab?


if you make an .exe (for testing purposes) it must contain a main() function.

 

Yes Matlab is correctly installed and since I only have one Matlab version the header-files are also the right ones. It's just a Matlab Engine problem here:

I compiled the Matlab Enngine sample engwindemo.c with the lcc compiler from Matlab. (Following the instructions http://www.mathworks.com/help/techdoc/matlab_external/f39903.html)

Then I tested the .exe-file wiith Dependency Walker that gave me this result:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module. (ADVAPI.DLL, LIBUT.DLL, USER32.DLL)

Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module. (MPR.DLL, SHLWAPI.DLL)


If I compile with Visual C++ I get the same message from Dependency Walker. And it's actually the same error with my .dll

 
Maybe on a Matlab forum chances are higher that you find somebody who has stumbled upon the same problem already, especially if it is reproducible with original matlab examples.