C++ dll design

 
Hello. I am studying programming in MQL4. I have a question whether the pattern from this article https://www.mql5.com/en/articles/5798 contains the correct syntax for the class written in C ++ for MQL4? I have a few problems and I do not know if they result from a poor dll design.
A DLL for MQL5 in 10 Minutes (Part II): Creating with Visual Studio 2017
A DLL for MQL5 in 10 Minutes (Part II): Creating with Visual Studio 2017
  • www.mql5.com
This article was created as a development of ideas from the earlier published article related to DLL creation with Visual Studio 2005/2008. The original basic article has not lost its relevance and thus if you are interested in this topic, be sure to read the first article. However much time has passed since then, so the current Visual Studio...
 
lenon083:
Hello. I am studying programming in MQL4. I have a question whether the pattern from this article https://www.mql5.com/en/articles/5798 contains the correct syntax for the class written in C ++ for MQL4? I have a few problems and I do not know if they result from a poor dll design.

This probably isn't the answer, but, based on the following comment in your related https://www.mql5.com/en/forum/328644:

The same .dll library for MQL4 (x86) and for MQL5 (x64), identical MQL code

You need different DLLs for MT4 and MT5 (assuming the default 64-bit installation of MT5). You can't use the same DLL file. The code can remain the same, but you need to compile separate x86 and x64 versions for MT4 versus MT5.

An opposite limitation applies to MetaTrader 4 DLLs: only 32-bit libraries are allowed, while 64-bit DLLs cannot be connected. Keep this in mind and create a suitable version for your platform.
 
lenon083:
Hello. I am studying programming in MQL4. I have a question whether the pattern from this article https://www.mql5.com/en/articles/5798 contains the correct syntax for the class written in C ++ for MQL4? I have a few problems and I do not know if they result from a poor dll design.

This is really a separate point but, regardless of whether it works, I would not pass strings back from a DLL in the way that this article describes. More or less everything else which MetaQuotes have published on the subject tells you to use an output buffer, as I described in  https://www.mql5.com/en/forum/328644, rather than writing back into string memory passed by reference.

For example, a recent public build of MT4 (v1212) completely broke string reference variables passed to DLLs. It was quickly replaced by a new public build (v1220) fixing the problem, but it's a sign that MetaQuotes don't really expect you to pass data back from DLLs in this way.

 
JC:

This probably isn't the answer, but, based on the following comment in your related https://www.mql5.com/en/forum/328644:

You need different DLLs for MT4 and MT5 (assuming the default 64-bit installation of MT5). You can't use the same DLL file. The code can remain the same, but you need to compile separate x86 and x64 versions for MT4 versus MT5.

In that case, everything is fine with my dll. I'm compiling the same code for MQL4 (x86) and MQL5 (x64). The answer is probably obvious but I'm new to the MQL environment. There are many examples on the internet, however, many of them are very old and most are out of date.
 

Hmm, if the code for MQL4 and MQL5 can be identical why the following example does not work in MQL4 ("Cannot find 'fnProject1' in 'Project1.dll"). In MQL5 works without a problem.

C++ dll

PROJECT1_API int fnProject1(void)
{
    return 22;
}

MQL4/5

#import "Project1.dll"
int fnProject1(void);
#import

void OnStart() {

   Print(fnProject1());
}


However, the following example works correctly in both versions.

C++ dll

PROJECT1_API int fnProject1(int x)
{
    return 22;
}

MQL4/5

#import "Project1.dll"
int fnProject1(int x);
#import

void OnStart() {

   Print(fnProject1(1));
}

If I pass any argument (int, wchar_t, ... to the function then it also works in MQL4.

 
lenon083:
In that case, everything is fine with my dll. I'm compiling the same code for MQL4 (x86) and MQL5 (x64). The answer is probably obvious but I'm new to the MQL environment. There are many examples on the internet, however, many of them are very old and most are out of date.

All you need to do is to change the active solution platform from 32 bit to 64 bit when compiling the DLL to make it work in 64 bit MT5. If you did not use 64 bit MT5, you would not need to do that either. No code change whatsoever needed. 

PS: if you compiled it as 64 bit DLL you can not use it from MT4 (MT4 is 32 bit). You need to have 2 separate dlls - 32 bit for MT4 and 64 bit for MT5

 
Mladen Rakic:

All you need to do is to change the active solution platform from 32 bit to 64 bit when compiling the DLL to make it work in 64 bit MT5. If you did not use 64 bit MT5, you would not need to do that either. No code change whatsoever needed. 

PS: if you compiled it as 64 bit DLL you can not use it from MT4 (MT4 is 32 bit). You need to have 2 separate dlls - 32 bit for MT4 and 64 bit for MT5

English is not my native language. If I wrote it wrong before, I'm sorry. That's how I do 1.dll (x86) is for MQL4 2.dll (x64) is for MQL5. I didn't write it for simplicity. Honestly, I started learning from MQL4 but I had problems with correctly performing examples. For comparison, I started doing examples on MQL5 and here they all work. That's why I ask in this forum and try to find out what I'm doing wrong.
 
lenon083:
English is not my native language. If I wrote it wrong before, I'm sorry. That's how I do 1.dll (x86) is for MQL4 2.dll (x64) is for MQL5. I didn't write it for simplicity. Honestly, I started learning from MQL4 but I had problems with correctly performing examples. For comparison, I started doing examples on MQL5 and here they all work. That's why I ask in this forum and try to find out what I'm doing wrong.

If the problem is that MT4 simply isn't loading the DLL, then you could diagnose it by creating a tiny C++ executable which does exactly what MT4 does:

  • Load the DLL using LoadLibrary()
  • Get the address of the function using GetProcAddress()

That will confirm which of the two parts of the process is failing, and the Windows error code may provide some small extra information. It won't solve the problem, but it will confirm where you should be looking.

 
JC:

If the problem is that MT4 simply isn't loading the DLL, then you could diagnose it by creating a tiny C++ executable which does exactly what MT4 does:

  • Load the DLL using LoadLibrary()
  • Get the address of the function using GetProcAddress()

That will confirm which of the two parts of the process is failing, and the Windows error code may provide some small extra information. It won't solve the problem, but it will confirm where you should be looking.

MT4 will not load the library if it is 64 bit. If compiled correctly there is no problem at all with DLLs in MT4

 
Mladen Rakic:

MT4 will not load the library if it is 64 bit. If compiled correctly there is no problem at all with DLLs in MT4

Well, yes, we've both already said that. But lenon083 seems sure that he/she is properly compiling an x86 version, and that the problem is therefore something different.  

 

I have repaired a mistake thanks for your help. The problem was in the dll syntax.

Must be:

Project1.h

extern "C" PROJECT1_API int fnProject1(void);

Project1.cpp

int fnProject1(void)
{
    return 22;
}
A simple mistake.
Reason: