String from C++ dll

 

Hello. I'm learning coding in MQL4. I'm having trouble getting a string from my C++ dll file. I tested in MQL5 works without a problem. Can somebody help me?

C++ code:

PROJECT1_API void fnReplaceString(wchar_t* text)
{
    wchar_t replaceText[] = L"987654321";

    memcpy(text, replaceText, wcslen(replaceText) * sizeof(wchar_t));
}

MQL4 code:

#import "Project1.dll"
void fnReplaceString(string text);
#import

void OnStart() {

string text = "123456789";
   
   fnReplaceString(text);
   Print("Replace: ",text);
}
 
lenon083:

I tested in MQL5 works without a problem. 

It may not continue to work in MQL5. I think you'd be relying on something which is more of an accident than a deliberate part of the way that MT5 interfaces with DLLs.

The way that both MQL4 and MQL5 expect you to return a string from a DLL is basically the same way that all such Win32 functions work: you provide a buffer (and its maximum size) for the DLL to write into.

For example:

// DANGEROUS simplistic version; caller ought to pass in the maximum size of "output", which the C++ code then honours and works within
void WINAPI fnReplaceString(wchar_t * input, wchar_t * output)
{
  wchar_t replaceText[] = L"987654321";
  memcpy(output, replaceText, wcslen(replaceText) * sizeof(wchar_t));
}

The output buffer is passed from MQL4/5 to the DLL as a ushort[] array and, after processing by the DLL, converted from an array to a string using ShortArrayToString(). Simple example:

#import "MyDll.dll"
  void fnReplaceString(string, ushort&[]);
#import

ushort outputBuffer[];
ArrayResize(outputBuffer, <whatever>);

fnReplaceString("Input string", outputBuffer);

string strOutput = ShortArrayToString(outputBuffer);

You can also pass back Ansi text instead of Unicode text from the DLL by providing the buffer as uchar[] rather than ushort[], and then using CharArrayToString() instead of ShortArrayToString().

 
JC:

It may not continue to work in MQL5. I think you'd be relying on something which is more of an accident than a deliberate part of the way that MT5 interfaces with DLLs.

The way that both MQL4 and MQL5 expect you to return a string from a DLL is basically the same way that all such Win32 functions work: you provide a buffer (and its maximum size) for the DLL to write into.

For example:

The output buffer is passed from MQL4/5 to the DLL as a ushort[] array and, after processing by the DLL, converted from an array to a string using ShortArrayToString(). Simple example:

You can also pass back Ansi text instead of Unicode text from the DLL by providing the buffer as uchar[] rather than ushort[], and then using CharArrayToString() instead of ShortArrayToString().

Thank you for your help. Everything works in MQL5. However, in MQL4 I have the error "Cannot find 'fnReplaceString' in 'Project1.dll". I tested earlier and always have this error when I use the reference symbol" & "in the MQL4 code. Somewhere I do a stupid error but I can't understand where. The same .dll library for MQL4 (x86) and for MQL5 (x64), identical MQL code .
Reason: