[fpc-pascal]Mapping Object Pascal Classes to C++ Classes

memsom at interalpha.co.uk memsom at interalpha.co.uk
Wed Nov 13 15:52:53 CET 2002


Hi All,

I found an interesting articla written by Rudy Velthuis on the above subject. 
Take a look at:

http://home.t-online.de/home/rvelthuis/cppobjs.htm

It works in Delphi like a charm. Is this possible in FPC?? I don't have FPC to 
hand to test it out. 

Try the following code: (NB. The c++ dll was compiled using Borland C++ 5.5.1, 
this may not work with gcc or other compilers..)

The C++ code is a little half-arsed btw, and uses stdio.h instead of 
iostreams.h for no good reason.

Matt

// C++ DLL
//Start

#include <stdio.h>

class AbstractTest
{
public:
   virtual void write(const char *text) = 0;
   virtual void writeLn(const char *text)= 0;
   virtual void free(void) = 0;
};

class TestClass: public AbstractTest
{ 
public:
   virtual void write(const char *text){ printf(text); };
   virtual void writeLn(const char *text){ printf(text, "\n"); };
   virtual void free(void){ delete this; };
};

#define EXTERN __declspec(dllexport) __stdcall 

extern "C" AbstractTest* EXTERN NewConsole(void){
  return new TestClass();
}

#pragma argsused int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long \
reason, void* lpReserved) { return 1; } 



//End

//Pascal Import Unit
//Start

unit testu;
interface
type
  TestClass = class
  private
    OldSelf: TObject;
  public
    procedure Write(text: PChar); virtual; cdecl; abstract;
    procedure WriteLn(text: PChar); virtual; cdecl; abstract;
    procedure Free; virtual; cdecl; abstract;
  end;

function NewConsole: TestClass; stdcall;

implementation

{ $l test.obj }

function NewConsole: TestClass; external 'Test.dll';

end.

//End

//Pascal console app
//start

{$APPTYPE CONSOLE}
uses
  SysUtils,
  testu in 'testu.pas';

var
  t: TestClass;
begin
  t := NewConsole;

  t.Write('hello ');
  t.WriteLn('there');
  readln;
end.

//End



---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/






More information about the fpc-pascal mailing list