I am attempting to execute a function in a C dll (R.dll) as follows:
[DllImport("c:\\Program Files\\R\\R-2.10.1\\bin\\R.dll")]
public static unsafe extern int Rf_mkCharLenCE(string str, int len, uint ceType);
,,,
string test = "Hello";
int r=0;
try
{
r = Rf_mkCharLenCE(test, test.Length, (uint)cetype_t.CE_UTF8);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.StackTrace);
}
which throws System.AccessViolationException - Attempted to read or write protedted memory.
The signature of Rf_mkCharLenCE in the c code is:
SEXP Rf_mkCharLenCE(const char *, int, cetype_t);
where SEXP is a pointer to a S-Expression struct and cetype_t is an uint.
It doesn't matter how the return type is specified. I have tried int, IntPtr, and void*.
Yes, allow unsafe code is checked.
What can I do to fix?
Russ