in c++ i have these below classes
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;
};
my dll method in c++ is like this
__declspec(dllexport) int __thiscall A::check(char *x,char *y,char *z)
{
temp=new B(x,y,z);
return 1;
}
code for B() constructor is below:
B::B(string x, string y,string z)
{
.......
}
below mentioned is my c# dll import
[DllImport("sour.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
public static extern void check(IntPtr val,string x,string y,string z);
the c++ build went successful with out any errors, but when i call this method from c# using dll import method i am getting this below error when i am trying to assign memory for “temp” class pointer. Below mentioned is the error.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Can any one please help on this. Thanks in advance.
I would assume exporting methods instead of class members is easier.
Please make sure that the DllImport uses the correct calling convention.
Using PInvoke Interop Assistant results in
Did you make sure that 32/64 bit version of your dll matches. If you run the x86 .net version (32bit) you can activate debugging of native and managed code and you should be able to set a break point in your C++ method to see what happens.