I am new to C# and .NET. I want to use my existing C++ dlls within .NET, so I was experimenting with interop. I don't know why this exception is raised. I know what that exception means, I read the MSDN, but not sure why that is raised.
My code is so.
The dll code is a simple function that takes a char*
.
extern "C" __declspec(dllexport)void test(char *text)
{
// these raise System.AccessViolationException
// char c = text[0];
// int n = strlen(text);
// char *copy = strdup(text);
}
The C# code is a simple class calling this function
namespace Interop_test
{
class Program
{
static void Main(string[] args)
{
char[] text = new char[10];
for (int i = 0; i < 10; i++)
text[i] = (char)('A' + i);
test(text);
}
[DllImport("interop_test_dll.dll")]
private static extern void test(char[] text);
}
}
The said exception is raised if the commented out code in the dll function is removed. Why?
How can (or must) I pass an array of data from C# to C++?
Where can I find more information about dealing with memory management during C++/C# interop?