C#调用C++的dll总是报错 Attempted to read or write protected memory.Attempted to read or write p……C#代码:[DllImport("duxie.dll",EntryPoint = "lala",CharSet = CharSet.None)]public static extern string lala();private void button1_Click(ob
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/26 06:35:46
C#调用C++的dll总是报错 Attempted to read or write protected memory.Attempted to read or write p……C#代码:[DllImport("duxie.dll",EntryPoint = "lala",CharSet = CharSet.None)]public static extern string lala();private void button1_Click(ob
C#调用C++的dll总是报错 Attempted to read or write protected memory.Attempted to read or write p……
C#代码:
[DllImport("duxie.dll",EntryPoint = "lala",CharSet = CharSet.None)]
public static extern string lala();
private void button1_Click(object sender,RoutedEventArgs e)
{
string a = lala();//////////此处报错
}
错误为:Attempted to read or write protected memory.Attempted to read or write protected memory.
另外,dll本身的返回值是char*
我是新手,希望大家多多指教,最好详细一点,我会多加分的,
C#调用C++的dll总是报错 Attempted to read or write protected memory.Attempted to read or write p……C#代码:[DllImport("duxie.dll",EntryPoint = "lala",CharSet = CharSet.None)]public static extern string lala();private void button1_Click(ob
建议动态库不要返回字符串指针.而是在参数中传递.
基本上没有见过动态库中返回字符串指针的.看看所有WIN32的API,应该是没有的
返回字符串是在C++中产生的,而C#的GC机制会试图销毁这个字符串
估计这时就出现你说的这个错误了
作为引参传递时,参数内存是以C#分配的,所以GC机制可以自动回收它的内存.
解释的可能不是很好,很简单,这个错误的意思应该 是:C#不能读写C++产生的内存.
当然,肯定有别的方法可以读写.不在本问题中了.