Discussion of article "Developing graphical interfaces for Expert Advisors and indicators based on .Net Framework and C#" - page 14

 

An example is given in the announcement https://www.metatrader5.com/ru/releasenotes/terminal/1898.

MQL5

  1. Added native support for .NET libraries with "smart" import of functions. Now you can use .NET libraries without writing special wrappers - MetaEditor takes care of it.

    To work with .NET library functions, it is enough to import the DLL itself without specifying specific functions. MetaEditor automatically imports all functions with which it is possible to work:
    .
    • Simple structures (POD, plain old data) - structures that contain only simple data types.
    • Public static functions, in the parameters of which only simple types and POD structures or their arrays are used
      .

    To call functions from the library, just import it:
    .
    #import "TestLib.dll"
    
    //+------------------------------------------------------------------+
    //| Script programme start function|
    //+------------------------------------------------------------------+
    void OnStart()
      {
       int x=41;
       TestClass::Inc(x);
       Print(x);
      }
    The C# code of the Inc function of the TestClass class looks like this:
    .
    public class TestClass
    {
       public static void Inc(ref int x)
       {
        x++;
       }
    }
    The script will return the value 42.

    Work on .NET libraries support is ongoing, and future versions are planned to expand their use.

  2. Support for working with WinAPI functions has been added to the standard library. Now you don't need to manually import libraries and describe function signatures to use operating system functions in an MQL5 program. It is enough to connect the header file from the MQL5\Include/WinAPI catalogue.

    WinAPI functions are grouped in separate files by purpose:

    .
    • libloaderapi.mqh - working with resources
    • memoryapi.mqh - memory handling
    • processenv.mqh - working with environment
    • processthreadsapi.mqh - work with processes
    • securitybaseapi.mqh - work with OS security system
    • sysinfoapi.mqh - getting system information
    • winbase.mqh - general functions
    • windef.mqh - constants, structures and enumerations
    • wingdi.mqh - working with graphical objects
    • winnt.mqh - exception handling
    • winreg.mqh - working with registry
    • winuser.mqh - windows and interface management
    • errhandlingapi.mqh - error handling
    • fileapi.mqh - file handling
    • handleapi.mqh - handle handles
    • winapi.mqh - connection of all functions (WinAPI header files).

    The binding works only with 64-bit architecture.


MetaTrader 5 build 1930: Плавающие окна графиков и .Net библиотеки в MQL5
MetaTrader 5 build 1930: Плавающие окна графиков и .Net библиотеки в MQL5
  • 2018.10.26
  • MetaQuotes
  • www.metatrader5.com
Теперь вы можете выносить графики финансовых инструментов за пределы окна торгового терминала. Это удобно при работе с несколькими мониторами. Например, на одном можно расположить основное окно платформы и следить за состоянием счета, а на другом — графики для оценки ситуации на рынке. Чтобы отделить график от терминала, отключите опцию...
 
Nextor Visual Studio 2022, created a form with a button, run it in MetaTrader 5, I get an error....

In Visual Studio community 2019 everything works as the author did, but when he writes:

"Each form has a public list of elements that it contains. Going through the list of elements, the method finds those that the controller is able to support and subscribes to the events it needs. If an element on the form is not supported by the controller, it will simply be ignored. "

Alas, if you place a SplitContainer or DataGridView on the test form, the form does not start at all, it is not clear why, but such a complex component as Chart STARTED.

I enclose a test example of what components can be used for modification (although the form starts!).

Files:
TESTGUIC.zip  706 kb
 

when trying to create a controller GuiController controller = new GuiController(assembly, form, m_global_events);
gives Unexpected exception: The key with this name already exists (for DataGridView) and the controller is NOT created (the form is created, probably it can be shown, but without the controller how to control it?).

once again everything is OK if there are no SplitContainer and/or DataGridView components.

 
Nikolai Karetnikov #:

maybe someone else will stumble

this is the place in the article

"As you can see, the compiler understands external enumerations defined in Net-libraries and knows how to work with them."

I don't know for what reason, but the compiler stubbornly refuses to understand C# enums, so we drag enum from C# into Mql5 and everything works.

Only, Vasily, you have placed the Buy and Sell buttons in the trading panel backwards ))

But in general, the coolest thing has turned out. Thank you for your hard work!)

I had the same problem.

I solved it a little differently - instead of ClickOnElement I wrote GuiEventType::ClickOnElement. And after that it compiles and works without any problems. But if this is the case, it is really unclear how it worked for Vasily. Perhaps I did something wrong...

But otherwise, the thing is really necessary and useful. It's a pity that MQL5 doesn't have something similar (inbuilt).

 
MQL_User #:

I had the same problem.

I solved this problem a little differently - instead of ClickOnElement I wrote GuiEventType::ClickOnElement. And after that it compiles and works without any problems. But if this is the case, it is really unclear how it worked for Vasily. Perhaps I have done something wrong...

But otherwise, the thing is really necessary and useful. It's a pity that MQL5 doesn't have something similar (inbuilt).

I had such an error:

@MQL_User, I did as you did: " ...instead of ClickOnElement wrote GuiEventType::ClickOnElement" - everything worked!

 
Mano #:

For MT4, We need to do some changes on MtGuiController.dll. like DLLEXPORT using RGiesecke.DllExport and marshalling input & output parameters.

This does not work for MT4. I tried without any results.

 
Pete #:

I'm relative new to MQL5 and I learned a lot from this article! Thanks very much!


I have one question. Have been struggling a bit for the past 2 days to figure this out down below.


I have successfully completed each step a couple of times even on another computer but everytime when I try to compile the code I get an error message.


The MQL compiler states : "Undeclared Identifier" for any of the enumerations down below.


If I "comment" these enumerations out from MQL5 then it works accordingly which is interesting since this means that the code works but can't read the enumerations which is my guess?

If someone can help me I would greatly appreciate :)


Thanks,

I read that you need to declare the enums as well in the MQH File as the same exact way that were declared inside the DLL.

 
Mano #:

For MT4, We need to do some changes on MtGuiController.dll. like DLLEXPORT using RGiesecke.DllExport and marshalling input & output parameters.

string ReceiveEvent(int event_n); // Returns JSON string.. Use JAson.mqh to Deserialize objects

How do I use this inside OnTimer()?

I am using the same approach as explained in the article

void OnTimer()
{
   CJAVal data;
   
   for(static int i = 0; i < EventsTotal(); i++)
   {
      data.Deserialize(ReceiveEvent(i));
      Print(data.["id"].ToStr());
        
   }   
}


My question is: what int do I need to pass inside the ReceiveEvent function?
Sorry for my ignorance but I don't get it.
 
Hello there!! It is very useful article since we are able to integrate dot net application into MT5/4.
A big thanks to the writer. 

I am not used to c# but vb. I tried to create a form with vb dot net and have it in the MT5 terminal.  But failed.

Can anyone please me? 
Is this connector only support c#? 
Any chance to work with vb.net??
Appreciate your help. 
Thanks in advance. 
 

Can you please tell me what is wrong with the GuiMtController.mq5 program that reports an error when running?

'GuiMtController.mq5'
ClickOnElement'-undeclared identifier
1 errors,0 warnings
Files:
k9un.jpg  160 kb