Discussion of article "Exposing C# code to MQL5 using unmanaged exports"

 

New article Exposing C# code to MQL5 using unmanaged exports is published:

In this article I presented different methods of interaction between MQL5 code and managed C# code. I also provided several examples on how to marshal MQL5 structures against C# and how to invoke exported DLL functions in MQL5 scripts. I believe that the provided examples may serve as a basis for future research in writing DLLs in managed code. This article also open doors for MetaTrader to use many libraries that are already implemented in C#.

Figure 2. COM Interoperability model

Author: investeo

 
Very useful article! Thank you for the detailed presentation.
 
Academic:

Very useful article! Thank you for the detailed presentation.

Yes, thank you.

Eh, Metaquotes tried to improve performance, to get away from byte-code... and we still stick all programs in it! )))

Question to the author: have comparative tests of execution time of pure MQL5 programs, DLLs and .NET monsters been performed? It would be interesting to have a look at them.

 
alsu:

Yeah, thanks.

Eh, Metaquotes tried to improve performance, to get away from byte-code... and we'll still stuff all programs into it! )))

Question to the author: have comparative tests of execution time of pure MQL5 programs, DLLs and .NET monsters been performed? It would be interesting to have a look at them.

I join the question. I am interested in execution time for MQL, VB.NET(managed), C++(managed/unmanaged), C#(managed/unmanaged). And another question for everyone. Where (except VB - it is possible there) can you use dynamic arrays to transfer the code from MQL5 to a faster programme or DLL without modifications. In VB everything is almost identical to MQL (operators for working with dynamic arrays), but what languages also have everything necessary?
 
hello,
I think
in principle it's very useful to open up possible resources and find this article is very intersting. But I'm not very experienced programmer and therefore I have some question.

From my point of view unmanaged code gives me the possibility to develop platform undepended. But if somebody use Metatrader, he got a defined platform.

What is the benefit with unmanaged code in generally and in combination with MQL5 ?  - Please correct me if I have misunderstood something!


 
smartwart:
hello,
I think
in principle it's very useful to open up possible resources and find this article is very intersting. But I'm not very experienced programmer and therefore I have some question.

From my point of view unmanaged code gives me the possibility to develop platform undepended. But if somebody use Metatrader, he got a defined platform.

What is the benefit with unmanaged code in generally and in combination with MQL5 ?  - Please correct me if I have misunderstood something!


Hi smartwart,

 

The benefit is that there is a great amount of already developed code in C# that you can use for trading purposes. You will read more in my next article.

 

cheerz,

Investeo

 

This is best thing ever! I love C# and I was looking for any possibility to get it working! Best news of the year! I think this was last time when I wrote EA in MQL5. I will place all logic in libraries and will use one template EA.mq5 for all my EAs (just they will be linked with different libs).

 Thank you! 

 
alsu:

Yeah, thanks.

Eh, Metaquotes tried to improve performance, to get away from byte-code... and we'll still stuff all programs into it! )))

Question to the author: have comparative tests of execution time of pure MQL5 programs, DLLs and .NET monsters been performed? It would be interesting to have a look at them.

What bytecode did they get away from? Did they have it in 4? There was just a slow compiler with no optimisation.

Under .NET there is no bytecode, at startup everything is compiled into native machine code, with optimisation for a particular processor. If time for the first compilation is critical, use ngen http://msdn.microsoft.com/ru-ru/library/6t9t5wcf.aspx.

And tests yes... would be interesting

I personally use only 2 of the described methods, I've only heard about the others. Really did not understand how COM-interop can help, well it was probably for completeness of the picture. The article is 5.

Ngen.exe (генератор образов в машинном коде)
Ngen.exe (генератор образов в машинном коде)
  • msdn.microsoft.com
Внимание Единственный сценарий, в котором необходимо устанавливать зависимость отдельно — это загрузка зависимости приложением с помощью отражения, например путем вызова метода . Важно Примечание Формирование образов для различных сценариев Например, при выполнении сборки в рамках...
 
VDev:

What kind of byte code were they getting away from? Did they have it in 4? There was just a slow compiler without optimisation.

MQL4 had bytecode, while MQL5 is compiled into bytecode with subsequent pre-compilation into native x86 or x64 before execution. MQL5 behaves like .NET

MQL5 as a language will be further developed (developers demand more functionality and better compatibility with C++), and its optimiser will be improved.

In the latest builds, it is now possible to include resources (.BMP and .WAV) directly into EX5 files via #resource, which allows you to create self-sufficient applications. This is a good foundation for developing and selling full-fledged applications for MetaTrader 5 platform. The beta version of the MQL5 Market will be released soon, where any developer will be able to sell their applications (after strict moderation), and all traders will have direct access to this shop.

 

Am I the only one who cannot compile templates for unmanaged export for VS C#?

I took the template from the article (it's not complete, it lacks dll) and supplemented it with libraries from R. Giesecke taken here . Giesecke libraries taken here http://sites.google.com/site/robertgiesecke/Home/uploads,

took the original template from R. Giesecke, also made a mixture of the two templates, nothing comes out.

When compiling the original template the compiler gives the following:

------ Построение начато: проект: UnmanagedExportLibrary8, Конфигурация: Debug Any CPU ------
Построение начато 15.03.2011 19:02:04.
CoreCompile:
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /define:DEBUG;TRACE /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /out:obj\Debug\UnmanagedExportLibrary8.dll /target:library DllExport\DllExportAttribute.cs UnmanagedExports.cs Properties\AssemblyInfo.cs
CopyFilesToOutputDirectory:
  Копирование файла из "obj\Debug\UnmanagedExportLibrary8.dll" в "bin\Debug\UnmanagedExportLibrary8.dll".
  UnmanagedExportLibrary8 -> d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\bin\Debug\UnmanagedExportLibrary8.dll
  Копирование файла из "obj\Debug\UnmanagedExportLibrary8.pdb" в "bin\Debug\UnmanagedExportLibrary8.pdb".
AfterBuild:
  ILDasm: calling 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe' with /quoteallnames /unicode /nobar /linenum "/out:c:\Temp\tmpF417\UnmanagedExportLibrary8.il" "d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\bin\Debug\UnmanagedExportLibrary8.dll"
  ILDasm: ildasm 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe' returned gracefully.
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\bin\Debug\UnmanagedExportLibrary8.dll : Export warning EXP0009: Platform is AnyCpu, creating binaries for each CPU platform in a separate subfolder...
  ILAsm: Found method: UnmanagedExportLibrary8.UnmanagedExports..method private hidebysig static float64 'AddDays'(float64 'dateValue', int32 'days') cil managed
  ILAsm: Removing RGiesecke.DllExport.DllExportAttribute from UnmanagedExportLibrary8.UnmanagedExports.AddDays
  ILAsm: Parsing 242 lines of IL took 66 ms.
  ILAsm: calling 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\ILAsm.exe' with /nologo "/out:d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\bin\Debug\x86\UnmanagedExportLibrary8.dll" "c:\Temp\tmpF417\UnmanagedExportLibrary8.x86.il" /DLL "/resource=c:\Temp\tmpF417\UnmanagedExportLibrary8.res"  /debug  
  ILAsm: ILAsm 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\ILAsm.exe' returned gracefully.
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : 
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembling 'c:\Temp\tmpF417\UnmanagedExportLibrary8.x86.il'  to DLL --> 'd:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\bin\Debug\x86\UnmanagedExportLibrary8.dll'
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Source file is UNICODE
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : 
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_CallingConvention
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_CallingConvention
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_ExportName
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_ExportName
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : 
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : ***** FAILURE ***** 
d:\Programming\Visual Studio 2010\Projects\UnmanagedExportLibrary8\UnmanagedExportLibrary8\DllExport\RGiesecke.DllExport.targets(8,5): error : 
     в RGiesecke.DllExport.Parsing.IlParser.RunILTool(String installPath, String toolFileName, String requiredPaths, String workingDirectory, String settingsName, String arguments, String toolLoggingCode, String verboseLoggingCode, DllExportNotifier notifier, Int32 timeout)
     в RGiesecke.DllExport.Parsing.ILAsm.RunCore(CpuPlatform cpu, String fileName, String ressourceParam, String ilSuffix)
     в RGiesecke.DllExport.Parsing.ILAsm.Run(String outputFile, String ilSuffix, CpuPlatform cpu)
     в RGiesecke.DllExport.Parsing.ILAsm.ReassembleFile(String outputFile, String ilSuffix, CpuPlatform cpu)
     в RGiesecke.DllExport.DllExportWeaver.RunILAsm(ILAsm ilAsm)
     в RGiesecke.DllExport.DllExportWeaver.Run()
     в RGiesecke.DllExport.MSBuild.DllExportTask.Execute()

СБОЙ построения.

Затраченное время: 00:00:01.85
========== Построение: успешно: 0, с ошибками: 1, без изменений: 0, пропущено: 0 ==========

When compiling other templates about the same.

System: W7 x64 SP1 Home Prem Lic.

IDE: VS 2010.

Google does not help (.

I created the project dll manually and supplemented it according to the recommendations of R. Giesecke, no result either.

Please help with advice.

Robert Giesecke
  • sites.google.com
Deployed as nuget package!no longer limited to C#!VS 2008 is no longer supported (it has no nuget support)Various fixes regarding path reslution for ildasm.exe, ilasm.exe and lib.exeexport names are escaped, so using il keywords like add won't be a problem any moreThe task will only execute when you have selected a specific CPU target (x86...
 
kPVT:

I have not compiled a template for the unmanaged export to VS C #?

Took a template from the article (it is not complete, not lacking dll) adds to the libraries of R. Giesecke taken here http://sites.google.com/site/robertgiesecke/Home/uploads ,

took the original template from R. Giesecke, as did a mixture of two templates, it does not work.

When compiling the original template compiler generates the following:

When compiling other patterns about the same.

System: W7 x64 SP1 Home Prem Lic.

IDE: VS 2010.

Google does not help (.

Dll manually create a project and complements it in accordance with the recommendations of R. Giesecke, the result is also unavailable.

Help pliz advice.

Hi kPVT,

Is there something Windows7 - XP backward compatibility mode ind Win7? If the dll works in 32-bit mode, it means it has to be recompiled to 64bits to make it work.

I will try to ask RGiesecke himself on stackoverflow.com.

Best Regards,

Investeo