first steps importing DLL and use a funtion of it

 

Hi,


I'm quite new to MQL5 and would like to exchange data with a socket server implemented in Python.

As I faced a lot of issues with the Socket of MT5 I decided to implement an additional layer for communication written as DLL in C++ exporting some functions to be used in MT5.

Unfortunately even a very basic test fails where I just try to return a string making use of it in MT5.

What I did in mq5 is

#import "build/Debug/mt5DllTest.dll"

   string mt5dlltest_msg();

#import


int OnInit()

  {

      string msg=mt5dlltest_msg();

      for (int i=0; i<5;i++) 

         Print(msg);

   return(INIT_SUCCEEDED);

  }

When debugging, the process seems to die once executing the mt5dlltest_msg() call.

The C++ implementation is the following:

#include "mt5dlltest.h"

#include <string>


int main(int argc, char* argv[]) {

const std::string msg = mt5dlltest_msg();

std::cout << msg << std::endl;

return 0;

}

Everything needed to test my set-up, like source, cmake configuration, mt5 including a test console programm calling the function shown is part of the attached zip archive.

Might it be, that I cannot make use of std::string objects to exchange data?

I started with the return value of a chonst char * but the console test program printed strange signs which I guess is caused by the heap management. Returning a pointer to a piece of memory allocated in a DLL might not be the best idea. 

I haven't developed C++ since ~10years, so I might have done some beginner errors.

Looking forward to get your comments. :)

Thanks in advance

laiki 

Files:
dllTest.zip  1153 kb
 

..., so I might have done some beginner errors.


indeed it was a beginners error :)

this page helped me solving the problem.

the attached archives shows how to copy strings from a DLL to MQL5

How to Exchange Data: A DLL for MQL5 in 10 Minutes
How to Exchange Data: A DLL for MQL5 in 10 Minutes
  • www.mql5.com
Now not so many developers remember how to write a simple DLL, and what are special features of different system binding. Using several examples, I will try to show the entire process of the simple DLL's creation in 10 minutes, as well as to discuss some technical details of our binding implementation. I will show the step-by-step process of DLL creation in Visual Studio with examples of exchanging different types of variables (numbers, arrays, strings, etc.). Besides I will explain how to protect your client terminal from crashes in custom DLLs.
Files:
 
laiki #:

indeed it was a beginners error :)

this page helped me solving the problem.

the attached archives shows how to copy strings from a DLL to MQL5

Did you manage to get the socket working during optimizations?

 
karp wak #:

Did you manage to get the socket working during optimizations?

No, that's why I started implementing the message transfer in C++ and want to call the DLL, which worked in a test application but unfortunately I haven't found the time to make use of it in MQL5 ...