How can I use a C++ class in Delphi? I am trying to use it through an abstract class. However it doesn't work as expected I get weird numbers from Age();.
Delphi:
program Test;
{$APPTYPE CONSOLE}
type
IPerson = class
function Age(): Integer; overload; virtual; stdcall; abstract;
procedure Age(const Value: Integer); overload; virtual; stdcall; abstract;
end;
const
DLL = 'Interface.DLL';
procedure FreePerson(const Person: IPerson); external DLL;
function CreatePerson(): IPerson; external DLL;
var
Person: IPerson;
I: Integer;
begin
Person := CreatePerson;
Person.Age(10);
I := Person.Age(); // I is not 10?
end.
C++:
extern "C" class _declspec(dllexport) IPerson
{
virtual void Age(const int Value) = 0;
virtual int Age() = 0;
};
class Person: public IPerson
{
private:
int FAge;
public:
void Age(const int Value){FAge = Value;};
int Age(){return FAge;};
Person(){ FAge = 0; };
~Person(){};
};
extern "C" _declspec(dllexport) IPerson* CreatePerson()
{
return new Person;
}
extern "C" _declspec(dllexport) void FreePerson(Person** obj)
{
delete obj;
}
Ifor a normal class. Everyone would expect an interface. And an interface is that what you should use for that__stdcallin your C++ code. If you are using only virtual methods and same calling convention on both sides I believe it should work.