How to pass arrays to C++, found C# DLL tutorial - page 2

 

I just tried it, you are right.


I always use ArrayCopyRates() to have all values conveniently in one array and so I never had the need to use ArrayCopySeries() with only the highs or lows and therefore I never even tried it. It will only point to a memory block that is allocated but completely filled with zero. This must be a bug (or inconsistency) in MT4/MQL4. There are a few more, for example you cannot pass int and double by reference, it will simply pass a null pointer if you try. It seems they implemented the bare minimum to be able to somehow use DLLs at all and then they simply stopped and didn't care about it anymore.

You can only pass

  • normal mql4 arrays (one or more dimensions)
  • the special 2-dimensional all-in-one rates array (but NOT the individual High[], Low[], etc. arrays)
  • indicator buffers
  • strings
  • int only by value, not by reference
  • double only by value, not by reference


However, you can access High[] and the others if you use the RateInfo struct from the example instead

and in MT4 do it like

double history[][6];
ArrayCopyRates(history, NULL, 0);


and pass this to the dll function as

double& history[][6]

you will then receive a pointer to a RateInfo array as it is done and used in the GetRatesItemValue() function in the example and access for example the high of a candle as rates[nitem].high and at least this will always work.

 

regarding question 1: am no C++ expert and have no C++ example and no C++ compiler installed to do any experimenting, I would have to use google myself, the answer is probably simple but I don't know it.

here are some examples: http://members.cox.net/doug_web/eh.htm

There are two mechanisms __try and __except for catching Windows SEH exceptions (access violations, zero division, etc) and try() throw() catch() for C++ exceptions. The latter can be made to catch also the former ones by using certain command line switches or by using the exception translator mechanism. MT4 will immediately crash whenever there happens an exception somewhere in (or below) your DLL and is not caught before it propagates to the .exe.

If you manage to get it running reliably without crashing then you need not worry about exception handling. I suggested it only as a tool to find out whether something with your interface to .NET is wrong (and what exactly is wrong) or whether it happens earlier. If it happens when MT4 calls your DLL and you mess up the stack because the function declaration itself is wrong/different then you can not catch it anyways, whatever you do.


You can do the same debugging without exception handling by selectively commenting out suspicious blocks of code and by inserting OutputDebugString() calls before and after suspicious operations (you cannot simply use print because you won't have stdout available) to trace program flow and see what happens before it crashes.

 
perhaps this can help
 

1. Some tutorial http://www.psnouvion.com/projects/view/mt4_sample_dll

2. You can't pass array by reference in an EA or Script, only in CI, I think MetaQuotes design it that way :( 

 
phi.nuts:


2. You can't pass array by reference in an EA or Script, only in CI, I think MetaQuotes design it that way :( 

You had better tell that to my EAs . . .  they would most certainly disagree.  :-) 
 
RaptorUK:
You had better tell that to my EAs . . .  they would most certainly disagree.  :-) 

My apology for not being specific in my answer. 

...

2. You can't pass array by reference in an EA or Script to C++ *.dll, you can do that only in CI, I think MetaQuotes design it that way :( 

Certainly you can pass array by reference in mql4/mql5 ;)

 
phi.nuts:

My apology for not being specific in my answer. 

Certainly you can pass array by reference in mql4/mql5 ;)

Ah I see,  thanks for the clarification.
Reason: