Passing string to c# dll and get string

 

Hello there,

Since the new build when passing a string to my c# dll I get buggy results.

My mql4 code:

#property indicator_chart_window

#import "ClassLibrary1.dll"
   string scanner(string pattern);
#import 
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
int start()
{
string pattern = "011011101101";

   string show = scanner(pattern);
   Print(show);

return(0);
}

My Dll should just return the same string. No manipulation right now.

The Output is always something like "0???????T?????" or just plain "0"

I think its because of unicode from mql4 but i dont know how to convert my string and then send it to my dll.

The code from my c# dll :

[DllExport("scanner", CallingConvention = CallingConvention.StdCall)]

public static string scanner(string pattern)
 {
 string returnpattern = pattern;
 return returnpattern;
 }

Can you please help converting my string for my dll.

Thanks in advance.

 
 
But what if my string is not a constant anymore
 
   string ToPassToDll = "";
   if (NewStringToPassToDll)
      {
      ToPassToDll = TheNewString;
      }

this is the best i can do without seeing your code

as a matter of fact, i don't wanna see it work it out use your imagination

 

StringtoCharArray return number of copied elements where i want plainly the text

 

what part isnt clear

StringToCharArray

Symbol-wise copies a string converted from Unicode to ANSI, to a selected place of array of uchar type. It returns the number of copied elements.

int  StringToCharArray(
   string  text_string,         // source string
   uchar&  array[],             // array
   int     start=0,             // starting position in the array
   int     count=-1             // number of symbols
   uint    codepage=CP_ACP      // code page
   );   

Parameters

text_string

[in] String to copy.

array[]

[out] Array of uchar type.


start=0

[in] Position from which copying starts. Default - 0.

count=-1

[in] Number of array elements to copy. Defines length of a resulting string. Default value is -1, which means copying up to the array end, or till terminal 0. Terminal 0 will also be copied to the recipient array, in this case the size of a dynamic array can be increased if necessary to the size of the string. If the size of the dynamic array exceeds the length of the string, the size of the array will not be reduced.

codepage=CP_ACP

[in] The value of the code page. For the most-used code pages provide appropriate constants.

Return Value

Number of copied elements.

 
I have no knowledge of C# - isn't it possible to pass the string as the [IN,OUT] parameter, like in C++?
 
Ovo:
I have no knowledge of C# - isn't it possible to pass the string as the [IN,OUT] parameter, like in C++?

Yes, it is, but the particular issue is probably that the default marshalling for a DLL export is Ansi rather than Unicode. Similar to https://www.mql5.com/en/forum/149586#913598.

(A related point is that returning string values from DLL functions is the easy route in C#, but isn't a brilliant idea because there's no officially documented way of allocating the string memory in such a way that MT4 then frees it. In both v500 and v600, it's preferable to provide the DLL with a buffer which it copies into, instead of returning a string.)

 

Thank you gchrmt4

Your Link saved the day.

No Conversion needed.

I can agree with your last statement, and i think someone already posted some snippets to work around it.

However i cant find it anymore.

 
gchrmt4:

Yes, it is, but the particular issue is probably that the default marshalling for a DLL export is Ansi rather than Unicode. Similar to https://www.mql5.com/en/forum/149586#913598.

(A related point is that returning string values from DLL functions is the easy route in C#, but isn't a brilliant idea because there's no officially documented way of allocating the string memory in such a way that MT4 then frees it. In both v500 and v600, it's preferable to provide the DLL with a buffer which it copies into, instead of returning a string.)


I see. In such case I would avoid strings.

Actually I do not play with strings for DLLs imports any more. The string buffer behaviour was so tricky in the former MQL4, that it is not worth time to trial/error.

Now I systematically convert strings to structures (or structure arrays) before passing them to any DLL, and vice versa, and I have no problem.

Reason: