下面是c++ dll类
class A
{
public:
int __thiscall check(char *x,char *y,char *z);
private:
B *temp;
};
class B
{
friend class A;
Public:
B();
B(string x,string y,string z);
~B();
private:
string x;
string y;
string z;
};
c++ dll方法定义如下
__declspec(dllexport) int __thiscall A::check(char *x,char *y,char *z)
{
temp=new B(x,y,z); //getting error at this point when i am assigning memory to temp
return 1;
}
c# dll导入是这样的
[DllImport("MyDll.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
public static extern int check(IntPtr val,string x,string y,string z);
c++ dll 构建工作正常,但是当 c# 调用 c++ dll 方法时,它看起来也不错,当它进入函数并在方法的第一行中,它尝试为已在类 A 中声明为类指针的临时指针创建内存B 是私有的。它给出的错误是
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.