C#与MQL集成
MetaTrader 5 MQL Integration Example
Example of how to call C++ / C# functions from MetaTrader 5 MQL Expert Advisors, Indicators, etc
SUPER IMPORTANT: Build both projects in x64 or MT5 won't see the exported functions !
CSharp
- Create regular class library (dll) with Visual Studio/MonoDevelop/Xamarin
- Include NuGet package UnmanagedExports
- Attach DllExport attribute to every public static method you want to export
[DllExport("Add", CallingConvention = CallingConvention.StdCall)]
public static int Add(int left, int right)
{
return left + right;
}
More examples (Section 4, working with arrays): https://www.mql5.com/en/articles/249
Visual C++
- Create Win32 project and from the wizard change project type from Windows Application to DLL
- Mark the functions you want to export:
extern "C" __declspec(dllexport)
int __stdcall MyFunc() {
return 42;
}
More examples (working with arrays): https://www.mql5.com/en/articles/18
MQL5
- Copy your DLL files to <terminal_path>/MQL5/Libraries. You can also change output of your project to that path
- Import DLL files and describe functions
// VC++
#import "MQLIntegrationVCpp.dll"
int GetTheAnswerOfEverything();
#import
// C#
#import "MQLIntegrationCs.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
int OnInit()
{
Print("############ Gateway initiated");
Print("Hello C#: 2+3 = ",Add(2, 3)); // 5
Print("Hello VC++: The answer is ", GetTheAnswerOfEverything()); // 42
return(INIT_SUCCEEDED);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现