DLL returning ANSI string: how to declare

 

Hello,

i have a custom DLL returning ANSI string. How to delcare this function in .mqh file please ?


const char& myfunc() - error 

const char[] myfunc() - error 

const char* myfunc() - error  

 

char& myfunc() - error 

char[] myfunc() - error 

char* myfunc() - error  

how to do this ?

Thank you 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events - Documentation on MQL5
 

Nobody knows how to do this ?

Have a DLL which returns ANSI strings (const char*).

I need to convert those into Unicode string for MQL5.

Function is delcared

 

 

string my_function(string buf,string id);

 

In c++ function is delcared

 

const char* __stdcall my_function(const char* x,const char* y)

 

In MQL5 code, i use these functions to make the conversion:

http://mqlmagazine.com/mql-programming/dll-hell-mql5-edition-unicode-vs-ansi/ 

 

The point is that SOMETIMES return string is correct and sometimes empty (like having leading zero).

The function above logs each input and output and the log shows there is a non empty string returned where MQL5 strings seems to be empty. 

Has anyone knowledge about ? Please do not point to the function wrong declared in DLL. This is correct.

It must be related how the function should be declared in MQL5 or there is some basic implementation of string in MQL5.

Thank you

 

Also, the desk never answer. ... 

DLL Hell, MQL5 edition : UNICODE vs ANSI | MQLmagazine.com
  • mqlmagazine.com
Many many years ago, when we were kids, in the beginning years of the crazy 90s, two languages were in battle in developer world. Pascal, with a down-to-earth, easy to understand syntax, well suited to a high level language, and C++, with a more cryptic, but faster to use syntax, well suited to its medium level. C++ won the battle, and...
 

Additional information:

The logfile of DLL shows this:

 

my_function;param0=;100=115;1150=4221485.57000000;1151=-13.77000000 param1=100 ret=115 

so it returns 115.

 

When calling in MQL5

 

string param0=U2A(";100=115;1150=4221485.57000000;1151=-13.77000000");

string param1=U2A("100"); 

string ret= my_function(param0,param1);

/// ret=115=115

so only devil knows why 115=115 returned instead of 115 (like log shows)

Finally, the string returned is empty or wrong or sometimes correct. 

 

 

 

Hi chinaski, 

Ever read this https://www.mql5.com/en/articles/18 ?

 

How to Exchange Data: A DLL for MQL5 in 10 Minutes
How to Exchange Data: A DLL for MQL5 in 10 Minutes
  • 2010.01.27
  • MetaQuotes Software Corp.
  • www.mql5.com
Now not so many developers remember how to write a simple DLL, and what are special features of different system binding. Using several examples, I will try to show the entire process of the simple DLL's creation in 10 minutes, as well as to discuss some technical details of our binding implementation. I will show the step-by-step process of DLL creation in Visual Studio with examples of exchanging different types of variables (numbers, arrays, strings, etc.). Besides I will explain how to protect your client terminal from crashes in custom DLLs.
 
onewithzachy:

Hi chinaski, 

Ever read this https://www.mql5.com/en/articles/18 ?

 

 

Hello,

thank you for link but it is no help . The DLL works perfect and proven when working with MT4 and ANSI strings and MT5 without strings. 

The only interesting thing of this link is how to pass strings:

_DLLAPI void fnReplaceString(wchar_t *text,wchar_t *from,wchar_t *to)

 

My dll uses const wchar_t* instead. Also, some return const wchar_t* (allocated of course).

 

When doing tests - so loading DLL in another c++ program - i can convert to ansi and vice versa - no problem.

Only with MQL5 - there is a problem. I did not figured out  what the reason was.

 

Summary:  Methods listed here 

http://mqlmagazine.com/mql-programming/dll-hell-mql5-edition-unicode-vs-ansi/ 

work not reliable in my case, that is getting ANSI string converted correctly (when returned from DLL function) and strings (MQL5) converted corretly into ANSI (when passed to DLL function).


I wrote my own conversion functions and it seems to work now (using MFC)

const char*     __stdcall UnicodeToAnsi(const wchar_t* us)
{       
        boost::recursive_mutex::scoped_lock l(dll._mutex_conversion);
        CString& as = dll._RetBuf.Get(""); // get reference to allocated ansi string
        USES_CONVERSION;
        as=W2A(us); // convert into ansi
        return as;
}

const wchar_t*  __stdcall AnsiToUnicode(const char* as)
{       
        boost::recursive_mutex::scoped_lock l(dll._mutex_conversion);
        _bstr_t& us=dll._WRetBuf.Get(as); // get reference to allocated unicode string
        return us;
}


 

You may also provide wchar_t* interface for your functions directly but then you need to implement different for MT4. My goal is a DLL which provides some service functions for MT4 as well as for MT5.

DLL Hell, MQL5 edition : UNICODE vs ANSI | MQLmagazine.com
  • mqlmagazine.com
Many many years ago, when we were kids, in the beginning years of the crazy 90s, two languages were in battle in developer world. Pascal, with a down-to-earth, easy to understand syntax, well suited to a high level language, and C++, with a more cryptic, but faster to use syntax, well suited to its medium level. C++ won the battle, and...