MQL datetime to C# DLL

 

Hi,

Almost at the end of getting all the basics working when using DLL.

But I have the last issue with passing datetime.

MQL alert results when printing DLL return and simply MQL. C# should print the same date as native MQL, but it doesn't.



C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpertAdvisor.Chart;
using System.Runtime.InteropServices;

namespace ExpertAdvisor
{
    public class Order
    {
        [DllExport("TestDate", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.LPWStr)]
        public static string TestDate(DateTime Date)
        {
            return "C# " +  Date.ToString();
        }
    }
}


MQL

#import "ExpertAdvisor.dll"
  string TestDate(datetime date);
#import

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
    Alert(TestDate(iTime(Symbol(), PERIOD_H1, 0)));
    Alert("MQL " + iTime(Symbol(), PERIOD_H1, 0));

}


/Hendrik

 
  1. Metatrader's datetime is a Unix time stamp (seconds since 1970)
  2. C#'s datatime is a structure.
              DateTime Struct (System) | Microsoft Docs
              .net - What does "DateTime?" mean in C#? - Stack Overflow
  3. They are not the same.
 
William Roeder:
  1. Metatrader's datetime is a Unix time stamp (seconds since 1970)
  2. C#'s datatime is a structure.
              DateTime Struct (System) | Microsoft Docs
              .net - What does "DateTime?" mean in C#? - Stack Overflow
  3. They are not the same.
Okey, I will try to convert them then :)
 
This works :)
 [DllExport("TestDate", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.LPWStr)]
        public static string TestDate(long Timestamp)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(Timestamp);
            

            return "C# " + dtDateTime.ToString();
        }
Reason: