Witam,
Szukałem ale nie znalazłem więc proszę o pomoc, w C# jestem bardzo zielony a muszę go użyć.
W Borland C++ mam napisaną dll-ke z interfejsem i funkcją zwracającą wskaźnik do obiektu interfejsu:
extern "C"
{
class TMyInterface
{
public:
TMyInterface() { }
virtual ~TMyInterface() { }
virtual int GetTest() = 0;
};
__declspec(dllexport) TMyInterfacee* CreateInterface();
__declspec(dllexport) void DestroyInterface(TMyInterface* Interface);
}
//---------------------------------------------------------------------------
class TImpl : public TMyInterface
{
public:
__fastcall TImpl() {};
int GetTest() { return 1; }
};
//---------------------------------------------------------------------------
TMyInterface* CreateInterface()
{
return new TImpl();
}
//---------------------------------------------------------------------------
void DestroyInterface(TMyInterface* Interface)
{
delete (TImpl*)Interface;
}
//---------------------------------------------------------------------------
Jak użyć eksportowany interfejs w C#? Z tego co znalazłem trzeba utworzyć również interface w C# który będzie mapował poszczególne funkcje:
namespace MyInterface
{
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyInterface
{
[PreserveSig]
int GetTest();
}
}
[DllImport("TestDLL.dll")]
public static extern IMyInterface CreateInterface();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr handle = CreateInterface();
if (IntPtr.Zero == handle)
{
}
else
{
}
}