I'm trying to call a c# function in a dll from an unmanaged 3rd party app - metatrader
I have followed advice from Calling C# dll from unmanaged code however, the example about marshalling strings does not work.
Note: I have successfully called the integer addition example from the reference (function "Add"), and it works end to end with no problems, so I know the issue is to do with strings. Ie, the "ReplaceString" function does not work. I have also looked atRobertGisiecke website, but there is not a string example there, or I am too dumb to figure it out.
The error message I get in metatrader is:
15:27:40 2009.11.10 00:01 MT4LibTest EURUSD,H1: function 'ReplaceString' call from dll 'Testme.dll' critical error c0000005 at 040B031B.
Platform is Windows Server 2012 (64bit) and I have compiled to x86 because Metatrader is an x86 program
One more thing: I am not very experienced in VS world, so Im hoping someone can be kind enough to help
Thank you
C# code:
[DllExport("ReplaceString", CallingConvention = CallingConvention.StdCall)]
public static int ReplaceString(
[In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder str,
[MarshalAs(UnmanagedType.LPWStr)]string a,
[MarshalAs(UnmanagedType.LPWStr)]string b)
{
str.Replace(a, b);
if (str.ToString().Contains(a)) return 1;
else
return 0;
}
Calling function (Metatrader):
#import "MT4Lib.dll"
int ReplaceString(string & str,string a,string b);
int Add(int x, int y);
#import
string str="A quick brown fox jumps over the lazy dog";
string stra = "fox";
string strb = "cat";
Print(str);
Print(ReplaceString(str,stra,strb));
Print(str);
EDIT: I should make clear that the metatrader API that allows one to write 'scripts' does not allow full C++ types. So there is no char, no wchar and certainly no pointers to these types. Only "string".