Problems Invoking Managed DLL in x64 Environment

 

Dear all,


I am finding a problem in invoking functions of a C# DLL from MT5 into Win7 64bit; everything runs smoothly into my x86 laptop, therefore I assume there is something stupid I miss, so here is my short story:


- Have this nice DLL, doing a lot of cool calculations, to be used into my Expert Advisor; the software is working well on my laptop (intel processor, 32 bit Win7 Pro). I came to a point I have to use more than 2 Gb memory and a quicker processor, so I decided to run the Advisor and DLL into my desktop PC, carrying AMD processor and 64 bit Win7 Ultimate. I moved the advisor files and I copied the x64 version of the DLL.


- This is when I started getting "unhandled exception 0xc00000fd in clr.dll" when invoking the DLL. I said: ok, let's debug the DLL code, but remotely attaching terminal64.exe (runing on the desktop PC) to the Visual Studio debugger (running on the laptop PC) makes terminal64.exe to unattendently stop, without any error message,


- So I said: no problem, let's try to catch the exception in order write the call stack into Windows eventlog... no success... "unhandled exception" continues,


- So I said: no problem, let's pass a in/out string to the DLL functios and write the checkpoint there... no success... "access to memory 0xfffffff" error happens.


...but: if the DLL function does nothing (e.g. immediately returns a fixed value), unhandled exception or access violations errors disappear. Seems like the errors happen when the DLL function returns back from the call to some other function (e.g. eventlog write or stringbuilder members).


Here is the current code:


// * * *  * C# DLL * * * * 

...

[DllExport("OpenNNForTraining", CallingConvention = CallingConvention.StdCall)]

      public static int OpenNNForTraining(   int A, int B, int C, int D,

                                             [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder debugMsg) {


debugMsg.Append("this is not working");  // if this line is removed, then the function works well (value 77 is returned)


return 77;

}


// * * * * MQL5 side * * * *

...

   string dbgMsg = "step1";

   Print("Try invoking the managed DLL, return code: " + 

      IntegerToString(OpenNNForTraining(A, B, C, D, dbgMsg)));

   Print("Error message " + dbgMsg);


Note this is the first call to the DLL, so no memory allocations or other weird action is made before involing the function.

The application behaviour is so strange, that I really think there is something I miss... somewhere...

...any hints? I really have no more ideas on how to solve this issue!


Thanks, 

Ronconao