MT4 build 600+ C# and DLL Strings

Back to topics list To post a new topic, please log in or register
avatar
2
dotpanic 2014.02.12 21:08
 

Hello,

Prior to build 600, I wrote a C# DLL that worked perfectly but it seems that build 600 broked my EA calls.

The C# DLL define some functions like this (using RGiesecke library):

[DllExport("loadAllDatabase", CallingConvention = CallingConvention.StdCall)]
public static bool testFunction(string testString)
{
writeToDebugFile(testString);
return true;
}

On the EA side, something like this is written:

#import "myLibrary.dll"
bool testFunction(string connectionString);

testFunction("Test string");


I understand that build 600+ now uses Unicode string instead of ANSI ones, but I have to admit that don't have a clue how to handle this. :)
Does anybody know?

Thank you!

avatar
2
dotpanic 2014.02.13 11:14 #
 

OK, solved!

I had to add MarshalAs directives to make it work again. Just like this:

[DllExport("loadAllDatabase", CallingConvention = CallingConvention.StdCall)]
public static bool testFunction([MarshalAs(UnmanagedType.LPWStr)] string testString)
{
writeToDebugFile(testString);
return true;

}

Quite simple in fact :)

avatar
1
lukucz 2014.02.21 18:02 #
 

I solved a similar problem, but with string return value.

For others:

[DllExport("getLastStatus", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string getLastStatus(int ECIndex) {
// ...
return "Your string";
}


avatar
11
jseparovic 2016.02.07 11:04 #
 
lukucz:

I solved a similar problem, but with string return value.

For others:

[DllExport("getLastStatus", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string getLastStatus(int ECIndex) {
// ...
return "Your string";
}


I tried this approach to returning a String using DllExport and it did not free the memory. If you're doing this every tick in Strategy Tester, you'll notice. Maybe a bug in DllExport?

 

Anyway, the workaround is to do the following (as per https://www.mql5.com/en/articles/249)

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

        public static void GetCommandName(Int64 ix, int id, [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder commandName)

 

And the MQL:

      string name = "";

      GetCommandName(ix, requestId, name);

 

Back to topics list  

To add comments, please log in or register