I've wrote several functions in C++ that take const char * strings as parameters, but I can't seem to find any match in C# (Mono latest windows version) that doesn't throw an exception saying there was an attempt to read protected memory.
Here's the test dll function I wrote,
DLL const char * _stdcall TestString(const char *buf)
{
LogManager::getSingleton().logMessage("Test String Called.");
LogManager::getSingleton().logMessage( buf );
return "Testing String.";
}
And here's the C# source that invokes the function.
I tried regular C# strings first, then based on a tutorial I tried StringBuilder, both to no avail.
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace AuroraC
{
class Program
{
static void Main(string[] args)
{
StringBuilder b=new StringBuilder(100);
b.Append("Test 1");
Console.WriteLine("Hello world!");
Console.WriteLine("hello word");
DLL.TestString( b );
}
}
class DLL
{
[DllImport("OgreMax.dll", EntryPoint="_InitOgre@0",
ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern void InitOgre();
[DllImport("OgreMax.dll", EntryPoint="_InitOgre2@0",
ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern void InitOgre2();
[DllImport("OgreMax.dll", EntryPoint="_TestString@4",
ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern StringBuilder TestString(StringBuilder str);
}
}