Discussion on "How to write a DLL for MQL5 and exchange data in 10 minutes". - page 6

 
GarF1eld:
Usewchar_t instead of char

The problem lies in the mql itself.

 
_DLLAPI void __stdcall demo(char *stream, char *buf){ buf=stream;}

What kind of code is this? Copying a pointer is just copying 4 bytes of memory containing the address.

In this case, memcpy can help you, if you take care of the allocation of the necessary buffer in the receiving line beforehand (in the MQL5 program).

 
stringo:

What kind of code is this? Copying a pointer is just copying 4 bytes of memory containing the address.

In this case, memcpy can help you, if you take care of allocation of the required buffer in the receiving line (in MQL5 program).

What has the pointer got to do with it? This code works perfectly in mql4. Not after migration to mql5.

Everyone here is giving advice, but I wish one showed a really good example of how to pass the string from mql5 to dll function.

 
antonix
:

...

Question

What's the problem? How to humanly pass a string from MQL to dll?

1) Thank you for your message. The error has been fixed. Please wait for an update.

2) You have been correctly told about wchar _t - the strings in MQL5 are Unicode.

3) What do you mean "When checking in visual debug"?

4) This is the correct way to use it:

//--- нужно обеспечить буфер строки для заполнения его в DLL
StringInit(b,256);
//--- вызываем DLL функцию
demo("test",b);
//--- печатаем результат
Print(b);

//--- в DLL
_DLLAPI void __stdcall demo(wchar_t *stream, wchar_t *buf)
  {
   //--- проверим указатель
   if(stream==NULL || buf==NULL) return;
   //--- максимум 256 символов
   wcsncpy(buf,stream,256);
  }
 

Thank you for helping struggling programmers like me. :) Your example has brought some clarity to my situation.

I have decided to write (or eighth, who may need it) function to convert string from wchar_t to good old char. This function may be useful to those whose projects have a lot of code that uses the char type. And since MQL5 passes only wchar_t, it's easier to convert at the input than to rewrite half of the dll code.

char* w2char(wchar_t* str){
      unsigned int lenght = wcslen(str)+1;
      char* ansi = new char[lenght];
      wcstombs(ansi, str, lenght);
      return ansi;
}
 
antonix:

Thank you for helping struggling programmers like me. :) Your example has brought some clarity to my situation.

I have decided to write the function to convert a string from wchar_t to good old char. This function may be useful to those whose projects have a lot of code that uses the char type. And since MQL5 passes only wchar_t, it's easier to convert at the input than to rewrite half of the dll code.

DLL functions that accept strings as char *, of course, take a pointer to the first element of the array of char type. This means that when describing the import of such functions in MQL5 program, we should use the char array as a string parameter.

For these cases, we have provided the StringToCharArray function that correctly converts strings in the national alphabet. The wcstombs function does not always do this correctly.

 

The article is interesting and useful.

1) The fact that the source code is included is cool. Heh, but where is the compiled DLL? Obviously, it's not a problem for a Megaproger to create it.

But what if I'm not a Mega and not a Proger?! :)

2) The article uses an MS compiler. I'm not friendly with it:) I have BC++. Can you post the source code tuned for it?

(IMHO, probably, it is always better to post sources for MS and BC, they are two leading companies, but their writing styles are different).

 

We try not to post potentially dangerous code (DLLs) so as not to make users nervous. Besides this is just a test example.

Unfortunately, I don't have Borland C++ compiler at hand. Try to adapt it yourself, it's less than one page of code.

 

yu-sha:

Aren't the developers setting their sights too high?

And why reinvent the wheel when everything has already been done a long time ago (C, Delphi, ...)?

I absolutely agree. For me, for example, a well thought-out COM interface or, within the framework of existing strategy of development towards FXRobot Developer Studio;-), at least the possibility of passing a pointer to MQL5 Call Back function in DLL would be quite enough. I may be wrong, but I have the impression that so far the functions can only be exported for internal use. I think we need to get down to earth and realize that MT as a development environment will never come close to Borland Delphi(Embarcadero RAD Studio), Visual Studio and many others. It's not because of developers' talent but because of huge resources required for development of a modern IDE. I don't speak about tremendous support of the above mentioned platforms by third-party vendors and other things. First and foremost, I would like to see the efforts of developers to create a perfect user interface, standard functionality and the ability to use MQL to implement a bridge between the user programs and the server, where the programs in MQL will act as the driver. Don't get me wrong, I don't want to take away the favorite game of novice programmers, but it seems to me that professional programmers and traders who want to get a perfect ready-made solution, rather than a DIY constructor, should be given a little more attention.
 

One should not do what seems right, but what is beneficial. Because profitability is what is right. And in this case (MT5+MQL5+services) - a huge profit (will be repaid tens or hundreds of times) for MetaQuotes and a huge profit for traders. Just not right away.

It's always good to look wide and ahead for at least 5 years. MetaQuotes has done just that.