How to debug a .NET DLL imported in EA



I have this .NET DLL that I made, called TradingSystem.dll, and I import it into an expert advisor in MT5 running on Windows 10 x64.

The EA code looks like this:

#import "TradingSystem.dll"
void TS_Initialize();

int OnInit()

The .NET code:

public static class Exports
    [DllExport("TS_" + nameof(Initialize), CallingConvention.Cdecl)]
    public static void Initialize() { ... }

I would like to be able to debug this in .NET. Here's what I tried:

  1. have the .NET code open in Visual Studio 2019 (Community edition in case it matters)
  2. build the .NET DLL and copy both the DLL and the PDB to c:\Users\<User>\AppData\Roaming\MetaQuotes\Terminal\24F345EB9F291441AFE537834F9D8A19\MQL5\Libraries\
  3. open MetaEditor, open the expert, add a breakpoint inside OnInit and start debugging on real data
  4. after the breakpoint is hit in MetaEditor, go to Visual Studio -> Debug -> Attach to process.. and select Terminal64.exe from the process list. At this moment the process crashes (termiinal64.exe).

I also tried to start the project with the debugger already attached by configuring the project to start Terminal64.exe on debug. This results in terminal64.exe showing this message: A debugger has been found running in your system. Please unload it from memory and restart your program.

What is the correct way to debug the .NET DLL?


It's by design. Metatrader shuts down when it detects the debugger. 

You'll have to find a way to debug .Net code without debugger. Try log files.


try again with compile x64 bit only

Visual Studio 2019 open project use  Class Library (.NET Framework)

and compile change Any CPU to x64 all project

(debug I install Microsoft Build Tools 2015)