Sign in Join

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

Mailing lists / fpc-pascal / 2014-01

c structs and pascal interfaces

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Andrew Haines

Hi,

I want to use c "interfaces" exposed as structs in fpc as interfaces. In particular I'm converting the OpenSLES header file to fpc.

Here's an example of an exported struct:
struct SLPlayItf_ {
SLresult (*SetPlayState) (
SLPlayItf self,
SLuint32 state

	);
	SLresult (*GetPlayState) (
		SLPlayItf self,
		SLuint32 *pState
	);
	SLresult (*GetDuration) (
		SLPlayItf self,
		SLmillisecond *pMsec
	);
	.....

};

I'm not very familiar with interfaces but here's what I know. There are two types com and corba.

I think I need corba so I don't have the ref and unref procedures.

I would translate this to:
{$INTERFACES corba}
ISLPlatItf = interface
   function SetPlayState(state: SLuint32): SLresult; cdecl;
   function GetPlayState(state: PSLuint32): SLresult; cdecl;
   function GetDuration(msec: PSLmillisecond): SLresult; cdecl;
   ......
end;

So now two questions:

Is corba the interface type I need?

Am I wrong to exclude the self parameter from the interface translation?

Thanks,

Andrew Haines

Jonas Maebe
to Andrew Haines
Andrew Haines wrote 18 lines
I want to use c "interfaces" exposed as structs in fpc as interfaces. In particular I'm converting the OpenSLES header file to fpc. Here's an example of an exported struct: struct SLPlayItf_ { SLresult (*SetPlayState) ( SLPlayItf self, SLuint32 state ); SLresult (*GetPlayState) ( SLPlayItf self, SLuint32 *pState ); SLresult (*GetDuration) ( SLPlayItf self, SLmillisecond *pMsec ); ..... };

If they are exposed as structs, you should translate them into records in FPC (with {$packrecords c}. The "member functions" are fields that are procedure variables, and you have to explicitly include the "self" parameters.

Corba (or other) interfaces are something different and you cannot use them to import arbitrary C structs, regardless of whether they contain function pointers.

Jonas

Andrew Haines
to Jonas Maebe
Jonas Maebe wrote 10 lines
On 28 Jan 2014, at 18:13, Andrew Haines wrote: > I want to use c "interfaces" exposed as structs in fpc as interfaces. In particular I'm converting the OpenSLES header file to fpc. If they are exposed as structs, you should translate them into records in FPC (with {$packrecords c}. The "member functions" are fields that are procedure variables, and you have to explicitly include the "self" parameters. Corba (or other) interfaces are something different and you cannot use them to import arbitrary C structs, regardless of whether they contain function pointers.

Okay thanks,

Andrew