I've read the posts about instantiating a class from a dll in C# and
then even accessing the class method via the
CallingConvention=CallingConvention.ThisCall. Now I have a Class
defined in the header dll which has a pure virtual method. Inside the
dll a specific class is created which then implements the pure
virtual. The base class, defined in the header is being returned to
the C# program. I cannot get the EntryPoint (via Depends) to any pure
virtual function. And if I give the base class a default
implementation, the call from the C# app will call the base
implementation and not the derived implementation. Any ideas?
i.e. The following will return 1 instead of 2. And I don't know how
to get 3
DllHeader.h
-----------
class BaseClass
{
virtual int PureVirtual_Method() = 0;
virtual int Virtual_Method();
}
extern "C" STP_API BaseClass * CreateSubClass();
DllHeader.cpp
-------------
int BaseClass::Virtual_Method() { return 1; }
extern "C" STP_API BaseClass * CreateSubClass()
{
return new SubClass();
}
InternalHeader.h
----------------
class SubClass
{
virtual int PureVirtual_Method();
virtual int Virtual_Method();
}
InternalHeader.cpp
------------------
int SubClass::PureVirtual_Method() { return 3; }
int SubClass::Virtual_Method() { return 2; }
Class.cs
--------
public class BaseClass : IDisposable
{
private IntPtr _this = IntPtr.Zero;
public BaseClass()
{
_this = CreateSubClass();
}
public int Virtual_Method()
{
return CppClass_Virtual_Method(_this);
}
[DllImport(@"CppClass.dll")]
private static extern IntPtr CreateSubClass();
[DllImport(@"CppClass.dll", EntryPoint="#1",
CharSet=CharSet.Unicode,
CallingConvention=CallingConvention.ThisCall)]
private static extern int CppClass_Virtual_Method(IntPtr
instance);
}