using System; using System.Text; using RGiesecke.DllExport; using System.Runtime.InteropServices;
namespace TEST { class TEST { [DllExport("Add", CallingConvention = CallingConvention.StdCall)] public static int Add(int left, int right) { return left + right; } } }
namespace RGiesecke.DllExport {
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] partial class DllExportAttribute : Attribute { public DllExportAttribute() { } public DllExportAttribute(string exportName) : this(exportName, CallingConvention.StdCall) { } public DllExportAttribute(string exportName, CallingConvention callingConvention) { ExportName = exportName; CallingConvention = callingConvention; } public CallingConvention CallingConvention { get; set; } public string ExportName { get; set; } } }
// код МТ4 //+------------------------------------------------------------------+ #import "TEST.dll" int Add(int a,int b); #import
谢谢,这篇文章很有用尊重!
如何从 MT 向 C# 传递字符串,我在文章中没有找到。请解释一下。
谢谢,这篇文章很有用尊重!
如何从 MT 向 C# 传递字符串,我在文章中没有找到。请解释一下。
使用非托管导出将 C# 代码公开到 MQL5 的 新文章已发布:
作者:investeo
您好investeo,很棒的文章!
我在搜索如何将 C# DLL 连接到 MQL5 代码时发现了这篇文章...这对我帮助很大!谢谢。
现在,我需要从一个与第三方服务器进行 WebSocket 连接的 DLL 中获取数据,并将来自服务器的实时数据发送到 MQL5,但我没有找到任何实现 MQL5 与 DLL 或 API 之间实时和双向通信的示例。
您能帮助我吗?
错误 2 C:\Users\Administrator\AppData\Local\Temp\tmp8B24\dllTest.il(58) : error : syntax error at token '{' in: { dllTest
这种错误会出现在中文版的系统中,原因是UnmanageExports在生成IL文件时,一些注释被生成为中文导致UnmanageExports无法正常工作。这个问题是不会在日文和英文的操作系统上出现的,解决方案是有的,你可以把你得操作系统换成英文的~
但是这样就太麻烦了对吧,这里就需要拿出小狐狸的东西来修复一下咯!
在下面的地址中下载小狐狸提供的Unmanaged Exports修复版本
https://www.noisyfox.cn/397.html
http://www.magecorn.com/p/280.shtml
详情请看上面两篇文章
楼主上面的代码怎么一直运行不了的呢
2017.04.28 23:48:08.774 Cannot find 'Add' in 'Testme.dll'
在编辑器导航器中打开 "显示所有文件类型",用鼠标抓取您的 DLL,然后将其扔到程序的 MQL5 窗口中。
结果可能会让您大吃一惊。
下午好我是 2018....
您能告诉我在哪里可以看到调用用 Net 编写的导出函数的示例吗?
您可以在 IJW 的帮助下使用 C++ stripper,然后纯粹通过编译器-链接器将所有内容合并为一个 DLL,不需要使用文章中描述的模板进行变态操作,在此进行简要描述 -http://stackoverflow.com/questions/26226958/include-managed-c-sharp-dll-into-unmanaged-c-dll-all-in-one-single-file。
好吧,用 RGiesecke 模板描述的方法--它是一个黑客程序,迟早会停止工作,顺便说一句,我也没有工作......但是,如果有人仍然想玩文章中描述的 IL 方法,还有另一种自行车,虽然,也不工作 -https://github.com/winch/winch.pinkbile.com-c-sharp/tree/master/dll_tool/dll_tool。
下午好。您能写一个在一个文件中逐步构建 c++/c++CLI/c# 的测试示例吗?和文章作者一样的简单示例。
请提供一个用 C# 编写的函数从 mt4(或 mt5)访问它的简单示例 ...........将 a 和 b 值传递给使用 Sharp 编写的函数,并将结果返回给 mt4(mt5) 在文章
https://www.mql5.com/zh/articles/249 中,作者编译的 dll 在 mt4 和 mt5 中正常运行! ,但当我尝试从作者的源代码中创建自己编译的 dll 时(在 VisualStudio 2010 中),该 dll 无法运行(写入错误 127) 所有 .NET 版本均已安装。
// код C#
using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
namespace TEST
{
class TEST
{
[DllExport("Add", CallingConvention = CallingConvention.StdCall)]
public static int Add(int left, int right)
{
return left + right;
}
}
}
namespace RGiesecke.DllExport
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
partial class DllExportAttribute : Attribute
{
public DllExportAttribute()
{
}
public DllExportAttribute(string exportName)
: this(exportName, CallingConvention.StdCall)
{
}
public DllExportAttribute(string exportName, CallingConvention callingConvention)
{
ExportName = exportName;
CallingConvention = callingConvention;
}
public CallingConvention CallingConvention { get; set; }
public string ExportName { get; set; }
}
}
// код МТ4
//+------------------------------------------------------------------+
#import "TEST.dll"
int Add(int a,int b);
#import
void start()
{
Comment(Add(3,2));
}
//+------------------------------------------------------------------+
我花了三天三夜上网搜索和实验,总的来说,该示例在 VS2017 中正常工作,我设法在 MT4 下编译并运行 .dll,但 .....在俄文版 Win10 中却无法运行。
我做了以下工作:用 Win7 - 64 安装虚拟机,不过是英文版;安装英文版 VS2017;只安装 C# 软件包;将模板从RGiesecke 复制到 VS2017 模板文件夹 。编译项目时,不要忘记检查平台--必须是 x86(用于 MT4)。在第一次编译时,我遇到了没有命名空间 RGiesecke 的 错误 ,于是我从项目中安装了 UnmanagedExports包。 编译时出现了一个错误 - 未找到 ildasm.exe,但 .dll 在 MT4 下可以正常运行。
问题可能出在 RGiesecke 的模板上,它在英文版 Win 和英文版 VS2017 中都能正常运行,而且 VS2017 中的所有路径都应默认设置。
下面是它的工作原理)
这种方法适用于 .NET 版本 5 吗?
我没有测试过,但我怀疑它是否有效。
MT4 很难与 C# 进行交互,总会有一些陷阱。
改用 MT5 更容易。
但如果您原则上想使用 MT4,那么作为一种选择 - 根据文章中的方法用 C# 启动 .dll,在其中用单独的线程启动任何 C# 代码并组织交换,我曾用这种方法启动过 64 位 C# 库