[Gambas-user] Declaration and use of external functions contained in a Structure

Tobias Boege taboege at ...626...
Mon Apr 21 11:19:42 CEST 2014


On Mon, 21 Apr 2014, Ru Vuott wrote:
> Hello,
> 
> I'm transporting a C code in Gambas that provides some functions of "Libvo-aacenc" API.
> The peculiar thing is that a "header" file of "Libvo-aacenc" provides functions "contained in a Structure" !
> 
> Here the declaration in "voAudio.h":
> 
> typedef struct VO_AUDIO_CODECAPI
> {
> 
> VO_U32 (VO_API * Init) (VO_HANDLE * phCodec, VO_AUDIO_CODINGTYPE vType, VO_CODEC_INIT_USERDATA * pUserData );
> 
> VO_U32 (VO_API * SetParam) (VO_HANDLE hCodec, VO_S32 uParamID, VO_PTR pData);
> 
> ...and so on....
> 
> }
> 
> 
> ----------------------------------------------------------------
> 
> 
> In the C code those functions are used as follows:
> 
> **********
> int main(int argc, char *argv[]) {
> 
> ...
> VO_AUDIO_CODECAPI codec_api = { 0 };
> ...
> ...
> ...
> 
> codec_api.Init(&handle, VO_AUDIO_CodingAAC, &user_data);
> 
> codec_api.SetParam(handle, VO_PID_AAC_ENCPARAM, &params)
> 
> ....
> ....
> ...etc...
> 
> }
> *************
> 
> 
> How should I declare in Gambas those external functions contained in the structure?  ...as Pointer ? But "where" ? In a Structure ? EXTERN will not be used ?
> 

Declaration is easy:

Public Struct VoAudioCodecApi
  MyCallback As Pointer
  ' ...
End Struct

To initialise those callback fields, Gambas needs to know with what you
initialise them, so you will need to declare those functions as Extern.
Generally, it's OK to have functions assigned to Pointer fields in Structs.

> And then "how" do I use those functions in Gambas code ? 
> 

You can't (AFAICS!). The only two things you can "call" in Gambas are
objects that implement a _call() method or methods of objects (or intrinsic
functions which are actually methods of a special hidden object). And Struct
fields are neither.

However, you could pass that structure to a C library which would be able to
call either function type (C or Gambas) successfully but I think you already
know that.

Therefore, if you don't want to or cannot write a component for what you are
doing, you could at least write a little component to work around this
limitation: one that calls function pointers for you. That's at least the
only solution I can think of...

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list