Discussion of article "Getting Rid of Self-Made DLLs" - page 2

 

The author of the article is very grateful for such an accessible presentation of information about IPC interoperability without bicycle dlls.

Working with memory seems to me rather complicated, but the approach is clear, except for some questions, I hope knowledgeable people will help to understand:

1. With the help of memcpy we copy a two-byte variable short into the array uchar[2], how is the information put into the array itself?

What format will the values at index 0 and 1 of the uchar array be in?

uchar dst[2];
short src = 13331;
memcpy(dst, src, sizeof(src));

uchar[0] = ?, uchar[1] = ?;

The value is split by byte by by byte and written into the array, ok ... I think I understand here.

I can not understand what values fall into this array and how to get the original value from them, the question is not that I can not display these values on the screen.


2. How to properly fill the uchar[4] array with values of different types when copying memcpy, for example:

uchar dst[4];
short src1 = 2;
short src2 = 13331;
memcpy(dst, src1, sizeof(src1));
memcpy(dst, src2, sizeof(src2)); // Overwrites an already existing value, i.e. I assume that when writing to dst, the address must be specified with an offset of 2 bytes after writing src1.

It feels like the answer is trivial and everything is done simply, but how to write it down correctly ?

 

Have you triedPrint?

 

The fourth example gives error : 'operator=' - illegal operation use SAMPLE_04.mq4 34 7

#property copyright ""

#property link      ""

#property version   ""

#property strict

//Пример 4. Копирование структур средствами MQL5

//---

struct str1

{

  double d; // 8 байт

  long   l; // 8 байт

  int i[3]; // 3 * 4 = 12 байт

};

//---

struct str2

{

  uchar c[ 8 + 8 + 12 ]; // размер структуры str1

};

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart(){

  str1 src;   // 

  src.d = -1; // 

  src.l = 20; //

  //--- заполняем параметры структуры

  ArrayInitialize(src.i, 0); 

  str2 dst;  //  

  //--- превратили структуру в байтовый массив

  dst = src; //   <----- Ошибка

}


I understand the idea about the memory section, but maybe the definitions are not quite right?

Can you tell me what is the reason?

Thanks in advance.
 
_SERG_:

The idea of a memory location is clear, but maybe there is something wrong in the definitions?

What is the reason?

Structures of different types cannot be copied anymore, MQL has removed this possibility.

use union

//Example 4. Copying structures by means of MQL5
//---
struct str1
{
  double d; // 8 bytes
  long   l; // 8 bytes
  int i[3]; // 3 * 4 = 12 bytes
};

//---
struct str2
{
  uchar c[ 8 + 8 + 12 ]; // str1 structure size
};

union str12 { str1 s1; str2 s2; };

//------------------------------------------------------------------
void OnStart()
{
  str12 src;
  src.s1.d = -1; // 
  src.s1.l = 20; //
  ArrayInitialize(src.s1.i, 0); 

  // src.s2 - byte array from s1
}
 
_SERG_:


uchar is wrong by the way, and there double is also suspiciously present there.

And by the way src should specify what exactly belongs to it and will be passed.

Someone has already corrected you just as you suggested. Good. Think. Good luck.

 
will it work for mql4 for today?
 
Seric29:
will it work for mql4 today?

The languages (MQL4 / MQL5) are completely the same now - the difference is in 2-3 functions missing in MQL4(ArrayPrint and something else small) and in "new features" for MQL5 - database, DirectX and OpenCL.


but the article was written 8 years ago, now MQL has become a strictly typed language, and to assign 2 structures (example in the article) you need to write a copy constructor or serialise the structure into a byte array and then back again.

 
Your article very big help to me, but how to copy the article did not mention a function pointer, because I need to put the callback function pointer passed as I don't know how to implement.
 
Example 4. Copying the structures by means of MQL5
struct str1
{
  double d; // 8 bytes
  long l;   // 8 bytes
  int i[3]; // 3*4=12 bytes
};
struct str2
{
  uchar c[8+8+12]; // str1 structure size
};
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
  str1 src; 
  src.d=-1;
  src.l=20;
  //--- filling the structure parameters
  ArrayInitialize(src.i, 0); 
  str2 dst;
  //--- turning the structure into the byte array
  dst=src; 
}

Assigning structs of different types doesn't work anymore (parameter conversion not allowed - variable of the same type expected).

But it would be possible to work with unions:

struct str1
{
  double d; // 8 bytes
  long l;   // 8 bytes
  int i[3]; // 3*4=12 bytes
};
struct str2
{
  uchar c[8+8+12]; // str1 structure size
};
union union1
{
  str1 src;
  str2 dst;
};

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
  union1 u; 
  u.src.d=-1;
  u.src.l=20;
  //--- filling the structure parameters
  ArrayInitialize(u.src.i, 0); 

  //--- the byte array representing the structure is in dst.c
  ArrayPrint(u.dst.c);
 
I wonder if it is possible to get a real pointer to a function. Pointers obtained using typedef work perfectly well inside mql program. But unfortunately I failed to pass them to the dll.