Managed (.NET) dll working from installation folder but not from terminal_data_directory\MQL4\Libraries - page 2

 
smimon:

I realise I'm a little late to the party here, but I just thought I'd share a tidier method of accomplishing this.

In the static constructor of the class that contains the [DllExport] methods, hook an event handler to AppDomain.CurrentDomain.AssemblyResolve - this will get called into every time the framework tries to resolve an external DLL reference.

Within this handler, you can split off the first part of the ResolveEventArgs.Name field to get the required DLL name, and then combine with Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) to get the full required DLL path to be used in conjunction with Assembly.Load

In this way, no extra method to set the MT4 library path is required, and no manual specification of DLLs to load using magic strings is required. 

Can you give an example?  Thanks.
 
Michael:
Can you give an example?  Thanks.

//C#

string DLLpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

See the latest and updated files attachment.

Copy MT4.DLL.Test.mq4 to \MQL4\Scripts\

Copy MT4.Test.dll and  MT4.Utils.dll to \MQL4\Libraries\

Sample:

 

Files:
 
Ruby Salton #:

Here is some more info.

The problem occurs when the EA is calling DLL A which is calling another DLL B. 

In this case only when the DLLs are in the installation folder it works as expected. But when the DLLs are in the libraries folder we get the exception.

Please find attached EA + c# projects 

Thanks 

did you find the solution to call dependant DLL file ?  I ran into the the same problem. In my  program, I'm trying to login Discord (the discord part works smoothly on regular C # app) using Discord.Net. but It gave me this result in the image.  If I remove the Discord section, it runs smoothly.  If you found the solution or anybody know how, please help me.  

Thanks

my MQL5 program:

//+------------------------------------------------------------------+

//|                                                       CSharp.mq5 |

//|                                  Copyright 2021, MetaQuotes Ltd. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"


#import "GuiMT.dll"

//#import "C:\\Users\\thien\\Downloads\\MQL5_CSharp-main\\MQL5_CSharp-main\\GuiMT\\bin\\GuiMT.dll"

#define RAND_MAX 32767

enum DLLEvents

  {

   none, //0

   refreshButtonClicked, //1

   Tab1Clicked, //2

   closeBtnClicked, //3

   valuesLoaded //4

  };


DLLEvents enumEvent = none;

string msgBack = "";


void OnStart()

  {   

   

   CreateRandomArray();

   MQL5CSharp::ShowForm("Form1");

   while(enumEvent != 3) //3 is closeBtnClicked

     {

      enumEvent = MQL5CSharp::ReadLastEvent();

      if(enumEvent == 1) CreateRandomArray();         

      if(msgBack != MQL5CSharp::ReadInternalMsg())  {  msgBack = MQL5CSharp::ReadInternalMsg();  Print(msgBack); }

      Sleep(100);

     }

  }

double MathRandomBounds(double minValue, double maxValue)

  {

   double f = (MathRand()/(double) RAND_MAX);

   double retValue = minValue + f * (maxValue-minValue);

   return retValue;

  }

void CreateRandomArray()

  {   

   double arr[10];

   for(int index=0; index<10; index++) arr[index]=NormalizeDouble(MathRandomBounds(0,10),1);

   MQL5CSharp::Load(arr);

  }

  
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.11.21
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

For anyone still struggling with this - this works for me:

using System;
using System.IO;
using System.Reflection;

public static class Mt4Export
{
     static Mt4Export()
     {
         AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
         {
             string dllName = $"{new AssemblyName(args.Name).Name}.dll";
             string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
             string assemblyPath = Path.Combine(folderPath ?? string.Empty, dllName);
             return File.Exists(assemblyPath) ? Assembly.LoadFile(assemblyPath) : null;
         };
     }
.......
.......
.......
.......
}

Thank you @smimon and @roman5

Reason: