Search code examples
c#mql5

How to Import C# DLL into MQL5 script


I want to import following C# code which works fine as a dll into MQL5. would you please help me how to do the binding? (I am using Visual Studio 2015).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Keygen
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }
}

The MQL5 code is:

#property strict

#import "Keygen.dll"
void Program();
#import
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Program();
  }
//+--

----------------------------------------------------------------+

after compiling in mql5 I get following the error:

Cannot find 'Program' in 'Keygen.dll'
unresolved import function call

Solution

  • I solved this problem by putting the 64bit dll file in "Libraries" folder of MT5 Terminal and changing the MQL code like this.

    #import "Keygen.dll"
    //+------------------------------------------------------------------+
    //| Script program start function                                    |
    //+------------------------------------------------------------------+
    void OnStart()
      {
     Program::Main();
      }
    //+------------------------------------------------------------------+