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().
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().

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
MQL4 code: