Return C# struct to MQL via DLL

 

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

 
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
 
Because MQL supports only fundamental types as return values.

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


 
Convert structure to string and then send string, receive string convert back to structure.
 
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.
 
amrali #:
Try to pass a ref to the structure

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

 
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: