如何从代码中调用DLL文件中的函数?
为什么第一个MessageBox()
工作而第二个不工作?
我不知道问题出在哪里。
MQL5
可以访问dll
文件吗?
我需要调用读取JSON
C#
函数。
MetaEditor中没有出现错误。
C# .dll
文件:
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; } } }
这是一个MQL5
代码:
#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 ); }
欢迎来到
野生
MQL
世界
如何测试DLL函数调用访问?
这是最简单的部分。 从DLL
内部测试它。 将stdout
上的一些输入/输出参数/值打印添加到每个DLL
函数源中,在调试阶段,您可以在此处进行所有必需的C#
自诊断。
MQL
-side还需要允许所有DLL调用,请检查MetaTrader Terminal 5设置:
[x] Allow DLL imports ...
语法很重要:逐项检查calling-signature(s)
MQL
文档声明了要使用的MessageBox()
的单个,明确的调用签名:
int
MessageBox(
string
text
, // message text
string
caption = NULL
, // box header
int
flags = 0
); // defines set of buttons in the box
); // defines set of buttons in the box
Parameters :
text
: [in]
Text,包含要输出的消息。
caption
= NULL : [in]
要在框标题中显示的可选文本。 如果参数为空,则EA标题中将显示“EA交易”名称。
flags
= 0 : [in]
定义消息框的外观和行为的可选标志。 标志可以是一组特殊标志的组合。 ( 加: 默认值== 0 ==MB_OK
)
Return Value :
如果函数成功执行,则返回的值是MessageBox()
返回码的值之一。 ( 其中包括 :{ IDOK | IDCANCEL | IDABORT | IDRETRY | IDIGNORE | IDYES | IDNO | IDTRYAGAIN | IDCONTINUE }
)
MQL
不是C#
MQL
-string实际上不是一个string
,而是一个struct
MQL
不会原谅任何一个细节:
应有的注意是必须的 :
MQL
文档说明:
字符串类型的内部表示是一个12字节长的结构:
#pragma pack(push,1) struct MqlString { int size; // 32-bit integer, contains size of the buffer, allocated for the string. LPWSTR buffer; // 32-bit address of the buffer, containing the string. int reserved; // 32-bit integer, reserved. }; #pragma pack(pop,1)
这是The Strange Answer
一次呼叫工作原因的The Strange Answer
。
MessageBox()
没有尝试访问它调用的任何内存位置,因为伪造的MQL-string-struct(ill) – 通过.size struct-component声明它自己的.buffer
内存区域(间接寻址)到因此,在这种特定情况下,访问具有0个字节的长度,因此没有存储区域 (根据定义,最终与某些其他存储器对象的地址空间冲突) 。
在MQL
领域工作了十多年之后,凭借蠕动的MQL
语言语法拥有超过几百人*的实践团队经验,我可能会说, “不要依赖于编译阶段没有报告错误” , MetaTrader Terminal在很多情况下都让我们无毛,即使代码是逐字逐句发布的文档。
随意查看MQL上的其他post,了解有关DLL集成噩梦的更多详细信息,以及有关进入分布式处理的好故事, GPGPU
-computing等。
关于JSON
最后评论
如果我要设计一个通过JSON
进行通信的体系结构,我会使用ZeroMQ DLL
分布式处理服务,这比使用另一个JSON解析器作为绿地项目更快地实现目标。