Discussion of article "How to Exchange Data: A DLL for MQL5 in 10 Minutes"

 

New article How to Exchange Data: A DLL for MQL5 in 10 Minutes is published:

Now not so many developers remember how to write a simple DLL, and what are special features of different system binding. Using several examples, I will try to show the entire process of the simple DLL's creation in 10 minutes, as well as to discuss some technical details of our binding implementation. I will show the step-by-step process of DLL creation in Visual Studio with examples of exchanging different types of variables (numbers, arrays, strings, etc.). Besides I will explain how to protect your client terminal from crashes in custom DLLs.

Author: Renat Fatkhullin

 

Good article, but it raises some big concerns.

5. DLL calls wrapper and loss of speed on calls

As already described above, every call of DLL functions is wrapped into a special wrapper in order to ensure safety. 
This binding masks the basic code, replaces the stack, supports stdcall / cdecl agreements and monitors exceptions within the functions called.

This work inevitably leads to delay of the calling function. 
Therefore, it isn't recommended to perform very frequent DLL function calls (hundreds or thousands of times per second) for small operations. 

It's better to make infrequent calls.

I am all for adding protection for the application but not at the expense of performance. This is another change from MT4 as exceptions in dlls cause MT4 to crash, but well written code should handle this scenario, I would much rather see articles on writing safe code in dll's to overcome these sorts of problems.  So now due to bad coding habits everyone has to pay a performance cost. 

This is very very bad if you have libraries of algorithms that are called on a tick bases, I would dare say render them useless.   Given alot of the feedback I have seen re MT5 isn't good mostly due to people needing to rewrite indicators etc. this is just another item that causes alot of frustration, and seeing as it does impact performance I would imagine others like myself that user extensive use of DLL's will just stay with MT4 for as long as posible and look for another platform where this isn't a problem.

 Why oh why could Metaquotes just not add a slightly different method for invoking dll's in a safe way. The developer could then choose to load via safe loading or performance loading and ensure they write good code that catches exceptions.

"It's better to make infrequent calls"  I mean seriously what sort of statement is that

 

I compiled the dll using visual c++ express 2008 but interestingly terminal doesn't catch exception, but crashes inside fnReplaceString.

My terminal build is 239. Any clues?




 
investeo:

I compiled the dll using visual c++ express 2008 but interestingly terminal doesn't catch exception, but crashes inside fnReplaceString.

My terminal build is 239. Any clues?

Can you send me ex5 and dll?
 
pfx:

Good article, but it raises some big concerns.

I am all for adding protection for the application but not at the expense of performance. This is another change from MT4 as exceptions in dlls cause MT4 to crash, but well written code should handle this scenario, I would much rather see articles on writing safe code in dll's to overcome these sorts of problems.  So now due to bad coding habits everyone has to pay a performance cost. 

This is very very bad if you have libraries of algorithms that are called on a tick bases, I would dare say render them useless.   Given alot of the feedback I have seen re MT5 isn't good mostly due to people needing to rewrite indicators etc. this is just another item that causes alot of frustration, and seeing as it does impact performance I would imagine others like myself that user extensive use of DLL's will just stay with MT4 for as long as posible and look for another platform where this isn't a problem.

 Why oh why could Metaquotes just not add a slightly different method for invoking dll's in a safe way. The developer could then choose to load via safe loading or performance loading and ensure they write good code that catches exceptions.

"It's better to make infrequent calls"  I mean seriously what sort of statement is that

 

It's no wonder that this community has difficulty getting decent articles.  Any good writer would not bother spending the time to write here.  Why should they?  They do something decent like show how to incorporate dlls and establish communication, then someone comes along and knocks them right out of the tree. 

Look from what I can see, he did a decent job on the article, so maybe the performance isn't where it should be.  BUT the question or issue here is, if it is so bad and you are a part of this community, why are you not attempting to code a solution to what you have found as an obvious performance issue? 

 It's all well and good that you bring it up, but it does no one anygood to complain about it, if they are not willing to present solutions or even suggestions on how to achieve the goal.

Did the article expose risk, yes, I think we even got a sample of it when another person says, hey I have downloaded it and I get this problem with it.  Which is exactly the problem that was discussed in the article.  I think if I were to expand upon this article, it would be like you said there PFX, show how to safely interact.  However, he did expose the risk, leaving plenty of room for someone like yourself to expand on it by the very concept of the topic of SAFETY. 

 One personal note to the author from me, when writting topics try to refrain from terms like newbie.  It is insulting and degrading, not a professional way to speak of those you wish to have follow your wisdom. 

 

I've been trying to figure out with this dll import functionality, if I will be able to import dlls written in other languages like C#. Is this possible? if not why not?

What is the difference between a C++ dll and a C# dll.

 
ToolMaker:

What is the difference between a C++ dll and a C# dll.

The main difference is design. c# dll assembly is used for storing managed code, and c++ dll contains native code.

But there is a trick that makes assembly importing possible: Inverse P/Invoke

As for me, i'm used to write c++\cli wrapper dll for that purpose or to write all the managed code using c++\cli.

 

Can You show me importing structure to the C++ library?

I tray to add this code to MQL5DLLSample.cpp:
------------------------
struct MqlTick

{
    INT64 Time;
    double Bid;
    double Ask;
    double Last;
    UINT64 Volume;
};

_DLLAPI MqlTick __stdcall MyTick(MqlTick &my)
{
    my.Bid = 1;
    return(my);
}
------------------------

Then, I add this code to MQL5DLL Test.mq5 (import section)

   MqlTick MyTick(MqlTick &tick);

and calling it in OnTick()

   MqlTick tick;
   SymbolInfoTick("GBPUSD", tick);
   MyTick(tick);
   Print("My tick: ",tick.bid);
------------------------

Compilation of the c++ library and EA is without errors.

After calling MyTick(tick) functio in terminal I obtain error: MQL5DLL_Test (EURUSD,M1)    Access violation write to 0x00000008



 

Hello and thank you for this useful article.

 

I've tried to create a small dll to exchange data between mql5 and mysql.

So I've followed the different steps and had some errors.

The dll is in c++

The first one was complied with minGW

The ex5 couldn't open it because it was not a 64 bit compiled dll

So I compile with minGW 64 bits

My first dll (which was very simple) works

It makes some addition etc..

When I try to use mysql library, I can compile to build my dll without any problem

But When I try to open the ex5 calling it I've got the following error:

Cannot open C:\user......\DLLNAME.dll (193)

Do you have any idea to fix that?

Many thanks  

 

Hello everyone,

great article !


but I was just wondering can anyone help me with doing thing described in this article kind of backwards : getting data (ticks or bars data) into C++ program from metatrader ??

 

I've got this working with a standard int array, but is it possible to pass a CArrayObj / CArrayDouble into Visual Studio C++? 

Reason: