PDA

View Full Version : cpp classes and delphi classes in dll's



noeska
02-03-2009, 07:46 PM
In an harddisk cleanup i (re)discovered an experiment from last year. Might be usefull.

Inspired by an article written by Max Kleiner ( http://www.delphi3000.com/articles/article_2708.asp?SK= ) i made an delphi hello world class example living inside an dll.

helloworldi.pas

unit helloworldi;

interface
type
THelloWorldI = class
public
function Say(): string; virtual ; abstract ;
end;

function CreateHelloWorld: THelloWorldI; cdecl; external 'HelloWorldLib.dll';

implementation
end.


helloworld.pas

unit helloworld;

interface

uses helloworldi;

type
THelloWorld = class(THelloWorldI)
public
function Say(): string; override;
end;

implementation

function THelloWorld.Say(): string;
begin
Say := 'This is a test';
end;

end.


helloworldlib.dpr

library HelloWorldLib;

uses
SysUtils,
Classes,
helloworld in 'helloworld.pas',
helloworldi in 'helloworldi.pas';

{$R *.res}

function CreateHelloWorld(): THelloWorldI; cdecl;
begin
result := THelloWorld.Create();
end;

exports
CreateHelloWorld;

begin
end.


usage:
place helloworldi in the uses
in var: helloworld: THelloWorldI;
create an instance with: helloworld := CreateHelloWorld();
next use it: helloworld.Say();
and you get an hello world from a class living inside an dll.

i have yet to try to call the delphi dll from cpp.

arthurprs
05-03-2009, 09:44 PM
if you make class functions abstract and stdcall, it will work (i already tested with D7 and CodeBlocks)