Return C# struct to MQL via DLL

Hendrik Piiriste  

Hi,

I would expect the following code to work. But MQL giving "'Order' - illegal returned type".

Any idea why?


C#

namespace ExpertAdvisor
{
    public class ExpertAdvisor
    {
        [DllExport]
        public static Order Order()
        {
            Order Order = new Order();
            Order.IsBuy = true;
            Order.IsSell = false;
            Order.TakeProfit = 100;
            Order.StopLoss = 50;

            return Order;
        }
    }

    StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct Order
    {
        public bool IsBuy;
        public bool IsSell;
        public double TakeProfit;
        public double StopLoss;
    }
}


MQL

  struct order
    {
        bool isBuy;
        bool isSell;
        double takeProfit;
        double stopLoss;
    };

#import "ExpertAdvisor.dll"
order Order();
#import

//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
      order test = Order();

  }

Best Regards,

Hendrik

amrali  
lastpunisher:

Hi,

I would expect the following code to work. But MQL giving "'Order' - illegal returned type".

Any idea why?



Best Regards,

Hendrik

Try to pass a ref to the structure
Dominik Egert  
Because MQL supports only fundamental types as return values.

You need to use pointers in c# and references in MQL as function parameters.


Mahrukh Aleem  
Convert structure to string and then send string, receive string convert back to structure.
Dominik Egert  
Converting to string is inconsistent. Do not use strings for binary data.

Strings have a stop byte. It won't work due to length detection issues.
Hendrik Piiriste  
amrali #:
Try to pass a ref to the structure

Could you give me an example? As I'm not sure how to do that.

Hendrik Piiriste  
Dominik Egert #:
Because MQL supports only fundamental types as return values.

You need to use pointers in c# and references in MQL as function parameters.


My concert is the return type of the DLL, but MQL complains when using struct as DLL return type.
Reason: