Create your own MetaTrader extension (dll) - page 3

 

Really a C++ question...external DLL for MT4

Hi all,

I have an external DLL that I've written in C++. However, I'm still quite new to C++, so I think that might be why I'm having this problem. I have another version I've written in C# that works fine, except to make C# DLLs callable by MQL I have to alter them, and this alteration appears to cause small memory leaks. I'm hoping someone here can help me out with the C++ version. Here's the story:

I've been slowly developing various libraries for programming trading strategies. One of the things I'm trying to do is develop a consistent set of services for various things like logging, locking (thread-safe access to resources), and event notification. So basically, I'm trying to pull MQL up by its bootstraps into a more modern programming paradigm. Anyway, currently I'm working on a library that will allow EAs to receive notification of various trade events (placed, filled, closed). These events could be as a result of trades taken by the event-subscribing EA or from other EAs. To accomplish this, I'm using an external DLL that basically facilitates communication between EAs via message passing. There is a publishing EA that just looks for new trade events and when one occurs, it puts a message onto a queue for the subscribing EA (each subscriber has its own queue).

So the problem is this...As long as it's only one message being passed at a time, all works fine. Obviously that's not acceptable at all though. When more than one message is passed at a time, what I'm finding is that the first message gets corrupted (it keeps returning the message as "1" when instead it should be something like "FILLED,1005167"). The second message is fine, but the first is not. I haven't tested more than two messages yet, but presumably we'd still see the same thing.

I'd love it if someone knew the answer to what it is I'm doing wrong and could shed some light on my problem. A few notes: 1) I'm eventually going to be releasing this code under Apache 2.0 and so this source file is also being put out there under Apache 2.0...I intend to release this to the community when it's working fairly smoothly. 2) I've repeatedly said 'queue' here, but if you look at the source code you'll see me using a vector instead, and basically using it like a stack. Originally I had a queue, but changed it to a vector just on the off-chance that I was doing something wrong with the STL queue that I wasn't aware of. No such luck. The real implementation will be a queue, but for now neither queue nor vector are working correctly for me.

Thanks in advance for any insight.

Brandon Wilhite

Files:
 

Since I was here, I thought I'd share my experience. What you are wanting to do can certainly be done, and I've done it many times myself.

When I first started writing DLLs for use in MT4 I couldn't ever get it to work in C++. I think the problem was/is that my project settings were always incorrect in Visual Studio (there's so d@$% many of them). Just recently I found an example from CodeGuru, which I was actually able to compile and use. Now I basically cleaned out that project and use it as a template.

In the meantime, I've been writing C# DLLs and using this excellent tool here. It basically takes the IL and alters it so that it can be called from unmanaged code. The only problem is that MT4 seems to exhibit a memory leak when doing this. I haven't definitively determined if that's due to MT4 or the altering of the IL, or what, but it happens.

Next I'll either be learning to write wrappers on C++ and/or just go ahead and learn C++. Hopefully that will address the memory issue.

Anyway, what you are wanting to do can definitely be accomplished. I've managed to write some pretty complicated DLLs for MT4 in C#, including things like remoting, raw sockets, http, WinForms. Pretty much anything you can do with .NET can be called from MT4 this way (you name it, and I've probably done it). And it's very reliable. The only problem is the relatively slow memory leak.

Magick:
Thanks Patrick

thats a very interesting idea to use the FTP.

however I do want to be able to interact with MT - to send the tick and candle info to the .net app and this app communicate back to MT on when to trade.

I found this that I thought may help - Simplified Wrapper and Interface Generator

However I am a rather entry level programmer, and dont fully appreciate what is involved in having a c++ wrapper middle layer.

I would be interested to hear your opinion, if you think this could be a useful tool to use to help bridge between the c++ and .net?
 
bwilhite:
Since I was here, I thought I'd share my experience. What you are wanting to do can certainly be done, and I've done it many times myself.

When I first started writing DLLs for use in MT4 I couldn't ever get it to work in C++. I think the problem was/is that my project settings were always incorrect in Visual Studio (there's so d@$% many of them). Just recently I found an example from CodeGuru, which I was actually able to compile and use. Now I basically cleaned out that project and use it as a template.

In the meantime, I've been writing C# DLLs and using this excellent tool here. It basically takes the IL and alters it so that it can be called from unmanaged code. The only problem is that MT4 seems to exhibit a memory leak when doing this. I haven't definitively determined if that's due to MT4 or the altering of the IL, or what, but it happens.

Next I'll either be learning to write wrappers on C++ and/or just go ahead and learn C++. Hopefully that will address the memory issue.

Anyway, what you are wanting to do can definitely be accomplished. I've managed to write some pretty complicated DLLs for MT4 in C#, including things like remoting, raw sockets, http, WinForms. Pretty much anything you can do with .NET can be called from MT4 this way (you name it, and I've probably done it). And it's very reliable. The only problem is the relatively slow memory leak.

bwilhite you are a life saver! That looks like a great tool. Do you, by chance, have a c# project that demonstrates communicating with MT from C#? What I am wanting to do is have my .net app tell MT when to trade. Or perhaps you can recommend a link that could help. Any help would be greatly appreciated.

 

Many many thanks!

 

Dll

Hi ,

Can the DLL be used to send and receive orders via an excel spreadsheet?

Where can i learn this aspect of the DLL.

Kind Regards

Latino

 
codersguru:
Anybody interested in creating his/her own MetaTrader extension (dll) may go to:

Create your own MetaTrader extension (dll) - Part 1

Create your own MetaTrader extension (dll) - Part 2

There'll be another part (or 2) which I'm writing them!

Hope you enjoy them!

Please, update your course to Visual C++ 2008 Express Edition

 
bwilhite:
Since I was here, I thought I'd share my experience. What you are wanting to do can certainly be done, and I've done it many times myself.

When I first started writing DLLs for use in MT4 I couldn't ever get it to work in C++. I think the problem was/is that my project settings were always incorrect in Visual Studio (there's so d@$% many of them). Just recently I found an example from CodeGuru, which I was actually able to compile and use. Now I basically cleaned out that project and use it as a template.

In the meantime, I've been writing C# DLLs and using this excellent tool here. It basically takes the IL and alters it so that it can be called from unmanaged code. The only problem is that MT4 seems to exhibit a memory leak when doing this. I haven't definitively determined if that's due to MT4 or the altering of the IL, or what, but it happens.

Next I'll either be learning to write wrappers on C++ and/or just go ahead and learn C++. Hopefully that will address the memory issue.

Anyway, what you are wanting to do can definitely be accomplished. I've managed to write some pretty complicated DLLs for MT4 in C#, including things like remoting, raw sockets, http, WinForms. Pretty much anything you can do with .NET can be called from MT4 this way (you name it, and I've probably done it). And it's very reliable. The only problem is the relatively slow memory leak.

Were you ever able to identify the source of this slow leak bwilhite? I'd appreciate your help in this area if you are still willing. At the moment, I'm looking for a simple mql dll that will allow me to send and receive messages via sockets. I'm familiar with the basics of c#, so if I could find a way to take advantage of the, all the better.

Thanks.

 
Magick:
bwilhite you are a life saver! That looks like a great tool. Do you, by chance, have a c# project that demonstrates communicating with MT from C#? What I am wanting to do is have my .net app tell MT when to trade. Or perhaps you can recommend a link that could help. Any help would be greatly appreciated.

Have you had any luck with this Magick? This is exactly what I am looking to do as well - have the .net app tell MT when and what to trade. Please let me know if you have found a solution.

I also appreciate and would like to hear more from bwihite. Do you have any sample c# dlls that you could share.

Thanks!

 

I've been working on my own DLL, and am running into the 127 error.

.cpp file contains (among other things):

MT4_EXPFUNC char* _stdcall DLLtest() {

return("testing!");

}

[/CODE]

header file contains:

#define MT4_EXPFUNC __declspec(dllexport)

MT4 file:

[CODE]

#import "mydll.dll"

string DLLtest();

Of course that's not ALL that they contain, but that's the pertinent information. Now, based on my understanding, I have everything there to try to call the function from MT4.

For some reason, it's just not "seeing" that it exists. I thought it was the fact my DLL lacked a .def file, but even after manually creating it one it still doesn't work. Also, I believe the MT4_EXPFUNC line means that the function will be exported anyhow, so a .def is unnecessary. The whole thing compiles fine, the EA runs fine right up until I try to call the function (as it can't see it) and then it stops.

I'm at my wits end. I really thought it was a .def issue, but the more I think about it, I don't believe it is.

BTW I'm using Visual C++ 2008 Express...

Thanks for any insight,

-Jason

 

I've got the same 127 error using Visual C++ 2008 Express. No solution yet, if I find one, I will post it.

Reason: