1

below is the c++ dll class

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 method definition is below

__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 import is like this

[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 build works fine but when c# calls the c++ dll method also it looks good and when it enters the function and in the first line of the method it try's to create memory for temp pointer which has been declared in class A as pointer of class B which is private. the error that it is giving is

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Keplah
  • 954
  • 2
  • 13
  • 26
krishna555
  • 223
  • 5
  • 18
  • 2
    You cannot pinvoke C++ instance methods. CallingConvention.ThisCall is not in fact implemented. It crashes because the class object was not created, the hidden *this* instance pointer is garbage. You need to write a managed wrapper in the C++/CLI language so that you can properly use the *new* operator to create an instance of this class and run the constructor. And destructor. – Hans Passant Jun 04 '12 at 18:07
  • possible duplicate of [C++/CLI Mixed Mode DLL Creation](http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation) – Hans Passant Jun 04 '12 at 18:08
  • @HansPassant Your comment is inaccurate. You **can** PInvoke C++ instance methods. However, you also need to PInvoke the constructor and the destructor for this to work. – Danny Varod Jun 04 '12 at 18:30
  • @HansPassant Also, this is not a duplicate of the question you linked to since it is not specific to C++/CLI (may be a duplicate of another though). – Danny Varod Jun 04 '12 at 18:32

2 Answers2

0

The __declspec(dllexport) should be on the class (e.g. class __declspec(dllexport) MyClass), not on its member methods.

The entry point should be a mangled C++ name (e.g. 2@MyClass@MyMethod?zii), not "check".
You can use Depends.exe to find the name.

qehgt
  • 2,972
  • 1
  • 22
  • 36
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • export is on its class so there is no need to do export on the methods? – krishna555 Jun 04 '12 at 18:24
  • Correct, it should be only on the classes, since the methods can not be called directly - only via the class (if static) or instance (if not static). – Danny Varod Jun 04 '12 at 18:29
  • ok now the class is an export one and the check method does not have any export to it and the entry point is "check" itself that i got from dependency walker. But i am still getting the same error. – krishna555 Jun 04 '12 at 18:31
  • You must also wrap the constructors and destructors of the C++ class (in a similar way) in C# for this to work. – Danny Varod Jun 04 '12 at 18:31
  • @krishna555 Change depends view to C++ names. Wrap (and call) constructor first. – Danny Varod Jun 04 '12 at 18:33
  • actaully this class has lot of methods in it so do i need to wrap all the methods in c# class or just constructors and destructors are good enough? – krishna555 Jun 04 '12 at 18:35
  • At least one constructor and destructor + public methods you need. – Danny Varod Jun 04 '12 at 18:43
  • i wrote the constructor and destructor in c# and called constructor before this "check" method call but it is giving the same error. – krishna555 Jun 04 '12 at 19:09
  • Did you pass the IntPtr you get from the constructor as the first parameter to the check method? – Danny Varod Jun 05 '12 at 08:33
  • --- ok i think what i did was not correct implementing the classes in c#. As class A is friend class of class B i am unable to convert them in to c#. Can you please give me brief code of how to convert the above mentioned class A and class B in my question? – krishna555 Jun 05 '12 at 13:20
0

I found the issue and the issue is with the check function in c++. The temp is supposed to be created like this.

int __thiscall A::check(char *x,char *y,char *z)
{
  A *xyz=new A();
  A->temp=new B(x,y,z); // doing this eliminates the issue.
  return 1;
}

Thanks to all who helped me out on this.

krishna555
  • 223
  • 5
  • 18