Why the first MessageBox()
works and the second doesn't?
I don't know where the problem is.
Does the MQL5
can access the dll
file?
I need to to call C#
functions that read JSON
.
No errors appear in MetaEditor.
C# .dll
file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestMe
{
class Test
{
// [DllExport("Add", CallingConvention = CallingConvention.StdCall)]
public static int Add(int left, int right)
{
return left + right;
}
public static int Sub(int left, int right)
{
return left - right;
}
public static double AddDouble(double left, double right)
{
return left + right;
}
public static float AddFloat(float left, float right)
{
return left + right;
}
}
}
this is an MQL5
code:
#import "TestMe.dll"
int Add( int left, int right );
int Sub( int left, int right );
float AddFloat( float left, float right );
double AddDouble( double left, double right );
#import
#property strict // MQL-syntax-mode-modifier == "strict"
int OnInit()
{ int k = 0;
MessageBox( k ); // this call works
k = Add( 1, 666 );
MessageBox( k ); // Doesn't work
return( INIT_SUCCEEDED );
}
Welcome to the
Wild
Worlds of
MQL
How to test a DLL-function call access?
This is the easiest part. Test it from inside
DLL
. Add some input/output parameters/values printing onstdout
into eachDLL
-function source and during the debugging-phase you have all the neededC#
-side self-diagnostics covered there.MQL
-side also needs to have all DLL-calls permitted, check the MetaTrader Terminal 5 settings:[x] Allow DLL imports ...
.Syntax matters: check calling-signature(s) item by item
MQL
documentation states a single, clear call-signature forMessageBox()
to be used:MQL
is not C#MQL
-string is not astring
in fact, but astruct
MQL
is not forgiving any single tiny detail:due care is a must:
MQL
documentation states:This is
The Strange Answer
to why the first call worked.The
MessageBox()
did not try to access any memory location on it's call, as the faked MQL-string-struct (ill)-declared by itself, via the.size
struct-component it's own.buffer
memory area ( addressed indirectly ) to have 0 bytes in length and thus no memory area ( ultimately colliding, by definition, with an address space of some other memory-object ) will be, in this specific case accessed.After more than a decade in
MQL
domain, with more than a few hundreds man*years hands-on team experience with creepingMQL
language-syntax, I may dare state, "do not rely on no errors being reported in a compilation phase", MetaTrader Terminal has made us hair-less in many circumstances, even when the code was following published documentation word-by-word.Feel free to check other posts on MQL to see more details on DLL-integration nightmares and also good stories about going into distributed processing,
GPGPU
-computing et al.Final remark on
JSON
If I were to design an architecture to communicate via
JSON
I would jump in withZeroMQ DLL
distributed-processing services, that would make your goal much faster, than building just another JSON-parser as a greenfield project.