Question on DLL ...... - page 6

 
 
Is the question still relevant to the author? I'm writing a dll for myself, I can give you a small example.
 
Yes it is !!! I've sorted out the dll thanks to you ! Thanks ! But there are things that I can not figure out how to put them in the dll !
 

Functions are called according to certain rules - calling convention. MT4 is guaranteed to work with functions that adhere to __stdcall (a special case of a rule variant). To be sure that the exported function uses the aforementioned rules, it should look like this: void __stdcall AnyFn();

2. I know two ways to make library function export - with __declspec(dllexport) and with def file (it's easier, otherwise you have to struggle with function decoding in addition). In VS to include def file in the project, first add to project files, then add file name to: project properties/composer/input/module definition file.

P.S: Example project with def file are located in: terminal directory\ experts\samples\DLLSample.



 

PASSING PARAMETERS TO A DLL FUNCTION

1. When passing string variable, an array of char (ANSI character, takes 1 byte) is passed to DLL.

#import "myLib.dll"

void fn(string var);

#import

DLL:

void __stdcall fn(char *pointer) {}


2. Passing int or double to the library will cause the variable to be copied, hence when you change the variable in the dll, there is no change in the dll:

#import "myLib.dll"

void fn(int var);

void fn(double var);

#import

DLL:

void __stdcall fn(int var) {}


3. To be able to change int and double variables declared in MQL, arrays should be passed:

#import "myLib.dll"

void fn(int var[]);

void fn(double var[]);

#import

DLL:

void __stdcall fn(int *var) {}

void __stdcall fn(double *var) {}


4. When passing an array from string, struct MqlStr is passed to the DLL:

МКЛ прототип:

#import "myLib.dll"

void fn(string var[]);

#import

DLL:
struct MqlStr
{
   int len;
   char *string;
};

void __stdcall fn(MqlStr *var) {}


P.S.

Whenever an array or a string (string) is passed to the DLL, it is actually a pointer to a memory section (pointer) that is passed to the library as follows

void __stdcall fn(int *pointer) {} // like this. This way you can work as an array, for example, pointer[4] = 4.

void __stdcall fn(int &reference) {} // or so. But we will get access to only one element. It is relevant when passing an array of one element into the library.

 
#import "myLib.dll"

void fn(string var);

#import

DLL:

void __stdcall fn(char *pointer) {}

I couldn't find a proper explanation for the * in front of the pointer, what is it for? Isn't it possible to do without it ?????

And a question on the way how to get text from a dll ???

 
VOLDEMAR:

I couldn't find a proper explanation for the * in front of the pointer, what is it for? Isn't it possible to do without it ?????

And a question on the way how to get text from a dll ???

You don't need to get text from it. At least not this year.
 

1. Transferring a string to the IDC:

DLL:
#include <string.h>
void __stdcall Temp(char *mqlStr)
{
   size_t mqlStringSize = strlen(mqlStr);
   char stroka[] = "DLL string";          // Эту строку передадим в MQL.
   if(strlen(stroka) > mqlStringSize)
      stroka[mqlStringSize] = '\0';       // Добавляем нуль-терминатор (обозначает конец строки).
   strcpy(mqlStr, stroka);
   return;
}

MQL:
#include "WaveCounter.dll"
void Temp(string str);
#include
int start()
{ 
   string str = "wwwwwsff";    // Задаем длину строки; Если длина будет меньше необходимого, то строка будет урезана.
   Temp(str); 
   Alert(str); 
   return;
} 
2. The * sign indicates that it is a pointer. In short, any variable has not only some value, but also an address where it is located in memory. Pointers can work with this address. This is an issue that needs to be well understood, I think you should check out the C++ books;
 
220Volt:

1. Transferring a string to the IDC:

2. The * sign indicates that it is a pointer. In short, any variable has not only some value, but also an address where it is located in memory. Pointers can work with this address. It's an issue that needs to be well understood, so you may want to check out some C++ books;
That's what I'm doing. I'm using C++, watching videos, reading books. Now much of MKL 5 is becoming clear .......
 

You just need to be careful with the global data in the library. The MCL programs run independently of each other (but share global variables because they are in the same thread), hence it is possible that several scripts will write and read to the same memory location (which is not good). This requires synchronization, e.g. by using critical sections. I emphasize that this is true for global data (declared outside functions). When functions are called, their own, independent set of variables is created.

Reason: