Hi. suppose a dll contains the following functions
extern "C" __declspec(dllexport) void f(bool x)
{
//do something
}
extern "C" __declspec(dllexport) const char* g()
{
//do something else
}
My first naive approach to use these functions from C# was as follows:
[DllImport("MyDll.dll")]
internal static extern void f(bool x);
[DllImport("MyDll.dll")]
internal static extern string g();
The first surprise was that C++ bool doesn't convert into C# bool (strange runtime behavior, but no crashes, though). So I had to change bool to byte and convert from one to another by hand. So, first question is, is there any better way to marshal bool (note that this is bool, not BOOL)
The second surprise was that the raw string returned by the dll function was OWNED by the C# string, not copied, as I would expect, and eventually the C# code frees the memory returned by the dll. I found this out because the program crashed, but then I changed the return type to sbyte* and manually initialized the string with that pointer which already does the copy. So the second question is: 2.1: Is there any better way to prevent the marshalled string from owning the pointer. 2.2: WTF?! Why does C# do that? I mean, an obvious case is when the dll func returns a literal, and C# tries to delete it...
Thanks in advance, and hopefully my questions aren't vague or incomprehensible.