Dll doesn't work from MT4

 

Hi there,

I have a tiny question and I hope someone here will be able and willing to help me out.

The basic issue is using external functions in MT4 via a DLL. My basic problem is that it doesn't work but doesn't give any errors either.

So let me be more specific:

I used the MATLAB C++ Shared Library to convert an m-file into a DLL. That worked fine. Then I wrote, or rather copied, a wrapper dll, which converts my input to the format understood by the matlab dll.

Anyway I end up having a dll which is supposed to provide one function

double GetMaxEigenValue( double s1, double s2, double s3 )

[/CODE]

I define the function as

__declspec(dllexport) double _stdcall GetMaxEigenValue( double s1, double s2, double s3)

[/CODE]

This should be fine.

In my .def file I have

LIBRARY "EV Arbitrage"

EXPORTS

GetMaxEigenValue

[/CODE]

Ok. I then tried to test what I did so far by writing a simple c++ script which uses this function:

[CODE]

#include

using namespace std;

double _stdcall GetMaxEigenValue(double s1, double s2, double s3);

void main(void)

{

double Ret = GetMaxEigenValue(5.,2.,3.);

cout << Ret;

}

This worked alright, giving me the impression that there is nothing wrong with my DLL.

However, now I tried to use the function from MQL4. SO that's what I did: I copied the dll, as well as the dll generated by matlab (not sure if that's necessary) into the experts/libraries folder, put the following script, "EV Arbitrage Library.mqh"

[CODE]

#import "EV Arbitrage.dll"

//double GetMaxEigenValue( double Rates1[][6], int MaximumRecords1, double Rates2[][6], int MaximumRecords2, double Rates3[][6], int MaximumRecords3, int z );

double GetMaxEigenValue( double s1, double s2, double s3);

#import

into the experts/include folder and tried the following in a Custom Indicator:

[CODE]

#include

...

Alert("before")

double val = GetMaxEigenValue(5.,2.,3.);

Alert("after");

Everything compiled just fine, but when I attached the indicator to a chart, it announced the first alert but not the second one. Everything behind the call to my dll seems to be geting evaluated.

I would appreciate any idea what I could be doing wrong If more code is needed I will provide it of course.

Thanks a lot

Pere

 

Two EDIT's: (I seem to not be allowed to edit my posts)

Of course, vverything behind the call to my dll seems to NOT be getting evaluated.

In my mql4 import section, the commented line was just another try I had, but can safely be ignored.

One idea I have, is the following:

When compiling the C++ script I had to link to the corresponding lib file. I didn't do that or something similar when calling the dll from mql4. (and wouldn't know how to anyway). Might that be the reason?

thanks again

 
Pere Callahan:
However, now I tried to use the function from MQL4. SO that's what I did: I copied the dll, as well as the dll generated by matlab (not sure if that's necessary)

If you linked mathlab.lib in your dll you must have it in your system.

However, put mathlab.dll in windows/system32 as it happens to me, whole thing wasn't working until I moved linked dll to system32.

(but buils your dll as static if possible)

check your experts tab to see if you get error 126 or error 127

Hope it helps.

 

Thank you very much for your response. I will try that later today.

So you think it's just about putting the *.lib file in the correct folder? Wouldn't we have to tell the mql4 compiler ( or linker if there is such a thing ) to actually look for it. Because when I compiled without the lib there was no error so apparently the mql compiler didn't expect to find it.. Am I right with this? I have to admit, my understanding of how these different compilers linkers, libraries and so on work together is rather limited.

I will keep you posted on the result of your suggestion.

Pere

 
Pere Callahan:
Thank you very much for your response. I will try that later today.

So you think it's just about putting the *.lib file in the correct folder? Wouldn't we have to tell the mql4 compiler ( or linker if there is such a thing ) to actually look for it. Because when I compiled without the lib there was no error so apparently the mql compiler didn't expect to find it.. Am I right with this? I have to admit, my understanding of how these different compilers linkers, libraries and so on work together is rather limited.

I will keep you posted on the result of your suggestion.

Pere

You have to put *.lib file only in linker. Mql compiler won't look for any *lib file.

Your dll will look for dll specified in linker. If your lib file is called mathlab.lib then you must have mathlab.dll in your sytem.

 

I see. I tried copying both dll's in the experts/library folder (without success), but I will try and put them in the system32 folder as well... even though for my test script it was enough to simply put the *.exe and the two dll's in the same folder.

Anyway I will be happy if the whole problem is simply about putting my files in the correct folder.

 

I don't know if is only a matter of correct folder... in library folder may or may not work, in system32 should work (dependencies to other system dlls - is OS is Vista check UAC or whatever is called Vista permissions system).

Check for errors in experts tab, create a simple function in your dll and call it from EA, put a alert window in your function to show result returned by that function.. well, some debug ideas...

 

It appears that it was the folder after all. I put the files in system32, and it went fine. Thanks for your help!

 

You're welcome.

 

How to get your DLL working

Hello everyone!

I took me some time to get external function calls to work, but the solution is very simple. I have tried this solution in both VC++ and Visual studio 2008 C++ project. A C# or VB created DLL will not work as simple, that has to do with managed and unmanaged code, google it.

Now here is the code:

* Enter this in your C++ code right after the dll initsialization;

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#define MT4_EXPFUNC __declspec(dllexport)

MT4_EXPFUNC int _stdcall Test(int indata)

{

return indata;

}

* Enter this in your .def file: If you forget u get that nasty error 127

EXPORTS

Test @1

* Enter this in your MQL4 advisor or indicator:

#import "Your dll name.dll"

int Test(int indata);

#import

Thats it!

 

DLL Call Error 4023

Hello everyone,

I was just wondering what error code 4023 means. I'm trying to call a C++ DLL function, however my expert adviser stops and returns 4023 according to GetLastError(). Just wondering what the code means as it is not listed on mql4.com nor will a google search or a forum search on this site produce any results.

Otherwise, if anyone knows of some sample code where price series arrays are passed to a C++ DLL it would be very helpful.

Thanks!

Reason: