unreadable text from external Win32 dll

 

Hi everyone,

I'm testing a custom dll that shur return a string when I call its function.

No problem while retrieving an integer, but text is represented in journal with a series of question marks.

I've tested the dll with a .net form and all is working fine.

Anyone can suggest me how to solve this issue ?

Thanks in advance.

Max.


here is the mql code (just for test the dll):

#property strict

#include <stdlib.mqh>

#include <WinUser32.mqh>

#import "MyDll.dll"

 string About(string txt);

 int Prova();

#import

string testo;

string txt;

datetime Lastcheck;



int OnInit()

  {

   Lastcheck=0;

   return(INIT_SUCCEEDED);

   Print("Ultimo controllo: " + TimeToString(Lastcheck,TIME_DATE|TIME_MINUTES));   

  }



void OnDeinit(const int reason)

  {



  }



void OnTick()

{

if (TimeCurrent() - Lastcheck > 30)

   {

      Print("Sono passati " + (TimeCurrent() - Lastcheck) + " secondi, chiamo dll");

      txt="xxxxxx";

      testo = About(txt);

      Print("Risultato chiamata About: " + testo);

      testo = Prova();

      Print("Risultato chiamata Prova: " + testo);

      Lastcheck = TimeCurrent();

   }   

}


and here is the journal text I get:


2018.02.21 21:18:34.281 testdll USDCHF,H1: Risultato chiamata Prova: 42

2018.02.21 21:18:34.281 testdll USDCHF,H1: Risultato chiamata About: ????????????????

2018.02.21 21:18:34.281 testdll USDCHF,H1: Sono passati 1970.01.01 00:00:31 secondi, chiamo dll


here is the dll c++ code

MyDll_API int __stdcall Prova(void)

{

    return 42;

}

MyDll_API char* __stdcall About(char* str)

{

char* res = (char*)::CoTaskMemAlloc(100);

strcpy(res, "Dll versione beta.");

return res;

}
 

Please use the </> button to insert your code.


 

I've not connected MQL to my own C++ library, but I would guess part of your issue is here:

string About(string txt);

You might want something like an array of uchar.

See this example:

https://www.mql5.com/en/articles/364#4

Getting Rid of Self-Made DLLs
Getting Rid of Self-Made DLLs
  • 2012.04.25
  • o_o
  • www.mql5.com
Do you still write your DLLs? Then we go to you! Introduction There always comes a moment when MQL5 language functional is not enough for fulfilling tasks. In that case an MQL5 programmer has to use additional tools. For example, it is possible to work with a database, use communication sockets or utilize operation system functions. An MQL5...
 

thank you Anthony Garot, I'll double check the article you linked.

have a nice day.


ml

 
Anthony Garot:

https://www.mql5.com/en/articles/364#4

What https://www.mql5.com/en/articles/364#4 doesn't appear to say is that there's a very strong chance that it leaks memory. In that example, libmysql.dll allocates a string buffer and passes it back as a function return value. The MQL4 code uses strcpy() and CharArrayToString() to convert it into a string which MQL4 can read. But it's unlikely that the original string memory from libmysql.dll gets freed at any point.

If - unlike the example at https://www.mql5.com/en/articles/364#4 - you control the DLL code, then it's better to use a similar but different approach. MQL4 allocates a buffer, and passes that to the DLL function. The DLL function puts its response into the buffer, rather than returning a pointer to a block of memory. In other words: the way that a Windows function such as GetSystemDirectory() works.

Reason: