I'm consuming a C++/CLR DLL to my C# app and been struggling with passing byRef arguments to the native methods, as the Native code comes with "&" but the C# prompts I need to remove the "ref" (or "out") keyword. Moreover, the arguments are translates to "" in my C# and strings for e.g. cannot accept that type so I'm getting conversion error from 'string' to 'string'.
I'm using Pavel Yosifovich example code for this demo:
Managed C++ file: ManagedPerson.cpp
String^ ManagedPerson::SayHello(String^ &greet) {
marshal_context ctx;
return gcnew String(m_pNative->SayHello(ctx.marshal_as<LPCTSTR>(greet)));
}
My C# App: Main.cs
static void Main(string[] args)
{
var person = new ManagedPerson("Bart", 10);
Console.WriteLine(person.SayHello("Hello "));
}
Then I'm getting this error in my C# compilation (cause of person.SayHello):
Argument 1: cannot convert from 'string' to 'string*'
This works if I remove the "&" from the C++ code (String^ &greet) but I must send it by reference.
I've managed to use types like Int32 using IntPtr successfully (though as unsafe code) but the problem is with strings and other complex types.
Any clue will help. Thx.