Using DLL in MQL, somethingh misterious

 

Hi all,

i'm here after a lot of hours spent on my pc attempting to understand how the import of DLL works with structures.

Some months ago i've easily created a class to interface my EA with MySql server using libmysql.dll without finding any difficult, since all functions uses a common mysql structure, wich doesn't need to be manipulated directly and can be referenced as an int.

Now i'm trying to get my EA opening a TCP connection with a remote server. Looking up over internet i've found a library to use windows sockets in MQL4. I tried to adapt the code to the MQL5, but a problem with a set of functions working over DLL defined stuctures still remains.


The questions are: how is possible to declare and manipulate structures defined in a DLL in MQL5? How can i use the integer reference of a structure?

Yes, this is the problem:

To allow the bind or connect function to be informed of wich port, address and family use you have to instantiate a structure of type SOCKADDR_IN and set it's internal members. So, while in libmysql.dll i don't need to manually create a struct of type mysql, now i need to create the SOCKADDR structure and manipulate it's members later.


Here are the signs of the functions and the sockaddr definition:

int bind(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)

int connect(int socket, const struct sockaddr *address, socklen_t address_len);


struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};



And this is a c++ example:

int gPort = 8780;

SOCKET lhSocket;

SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
    cout<<"Socket Initialization Error. Program aborted\n";
    return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
    cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}

memset(&lSockAddr,0, sizeof(lSockAddr));

lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("IPAddress");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));


if(lConnect != 0)

{
      cout<<"Connect Error. Program aborted\n";
      return;
}



Thanks for help!


Regards

Gianluca

Create Your Own Trading Robot in 6 Steps!
  • 2012.03.22
  • MetaQuotes Software Corp.
  • www.mql5.com
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
 

Please read article How to Exchange Data: A DLL for MQL5 in 10 Minutes and Using WinInet.dll for Data Exchange between Terminals via the Internet

If you want to call a function сonnect  with structure sockaddr_in, then it makes no sense, because in MQL5 pointer operation and types convertion sockaddr_in to sockaddr cann not be perform.

You can work with this function only in C++ code.

  

 
Please look at my code Working with sockets in MQL5
 
avoitenko:
Please look at my code Working with sockets in MQL5

Hi avoitenko,

thanks for your very fast solving reply. I've already downloaded your code and expanded with some other functionalities.

As far as i can see, you can also probably solve me another question directly related to C++. Indeed i'm writing a program to interact with the EA i'm developing, via socket.


Now i've the particular need to be able to load a possible unstable code, wich may causes mem leaks or segfault, from some libraries.

Is there a way to prevent the process to crash if a segfault occurs in the thread that loaded the dangerous code without causing an undefined behavior of the whole app ?

Is possible, in the same way, avoid memory leaks after the library unload ( and thread termination, not process end ) ?


I know it's not related to this topic, but i hope you may help me! I've written on a large number of forums without a relevant reply.


Regards

 
gianks:

Now i've the particular need to be able to load a possible unstable code, wich may causes mem leaks or segfault, from some libraries.

Is there a way to prevent the process to crash if a segfault occurs in the thread that loaded the dangerous code without causing an undefined behavior of the whole app ?

Is possible, in the same way, avoid memory leaks after the library unload ( and thread termination, not process end ) ?

  Can you post here the problematic code, so  understand the problem?