[fpc-pascal] Can I store an object reference in C as void pointer?

Guillermo Martínez Jiménez gmjimen at burdjia.com
Tue May 20 16:56:40 CEST 2008


I'll create a library in Object Pascal. I'll compile it in a
DLL/so/dynlib and want to define a public interface to be used in any
language so I decided to define it as "C public functions".

I'll use objects so some functions of the library should return or
receive object references but I'm not sure which C data type should I

As example, in pseudo Object Pascal:



  EXPORT FUNCTION CreateObject: TTheObjectClass; CDECL;
  EXPORT PROCEDURE UseObject (Obj: TTheObjectClass; Param: INTEGER); CDECL;
  EXPORT PROCEDURE DestroyObject (Obj: TTheObjectClass); CDECL;

And the pseudo C header:

/* theLib.h */
typedef TTheObjectReference void*;

extern TTheObjectReference CreateObject (void); pragma __DLL__ ("theLib");

extern void UseObject (TTheObjectReference Obj, long int Param);
pragma __DLL__ ("theLib");

extern void DestroyObject (TTheObjectReference Obj); pragma __DLL__ ("theLib");

Then, in a pseudo C program:

#include "theLib.h"

void main (void)
  TTheObjectReference Object;

  Object = CreateObject ();
  UseObject (Object, 1);
  DestroyObject (Object);

Is this correct or should I use a different approach?


More information about the fpc-pascal mailing list