c code
extern "C" __declspec(dllexport) int export(LPCTSTR inputFile, string &msg)
{
msg = "haha"
}
c# code
[DllImport("libXmlEncDll.dll")]
public static extern int XmlDecrypt(StringBuilder inputFile, ref Stringbuilder newMsg)
}
I got an error when I try to retrieve the content of newMsg saying that I’m trying to write to a protected memory area.
What is the best way to retrieve the string from c to c#. Thanks.
Using DLLs with exports that take C++ classes as their argument is dangerous even in C++. It is impossible to interop with C#. You cannot use the same memory allocator and you can’t call the constructor and destructor. Not to mention that your C++ code isn’t valid, it doesn’t actually return the string.
Use a C string instead. Make it look like this:
Some notes with these code snippets: