在 C++ 中,我有以下这些类
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);
return 1;
}
B() 构造函数的代码如下:
B::B(string x, string y,string z)
{
.......
}
下面提到的是我的 c# dll 导入
[DllImport("sour.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
public static extern void check(IntPtr val,string x,string y,string z);
c++ 构建成功,没有任何错误,但是当我使用 dll 导入方法从 c# 调用此方法时,当我尝试为“temp”类指针分配内存时,出现以下错误。下面提到的是错误。
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
任何人都可以帮助解决这个问题。提前致谢。