Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-05

Can I store an object reference in C as void pointer?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Guillermo Martínez Jiménez

Hello.

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 use.

As example, in pseudo Object Pascal:
_______________________________________

LIBRARY theLib;
USES
  unitObject;
  EXPORT FUNCTION CreateObject: TTheObjectClass; CDECL;
  EXPORT PROCEDURE UseObject (Obj: TTheObjectClass; Param: INTEGER); CDECL;
  EXPORT PROCEDURE DestroyObject (Obj: TTheObjectClass); CDECL;
END.
________________________________________

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?

Thanks.

Reply by email