Autore Topic: Dichiarazione ed uso di funzioni esterne contenute in una Struttura  (Letto 238 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.305
  • Ne mors quidem nos iunget
    • Mostra profilo
Talvolta, nel mio trasportare codice C in codice Gambas ed utilizzando librerie esterne, mi sono imbattuto in funzioni esterne che nei file header di tali librerie erano contenute e dichiarate in Strutture. In particolare esse erano dichiarate come fossero Puntatori.
Ultimamente ho trovato nuovamente questo caso, ma oltre a sapere di dover dichiarare in Gambas tali funzioni, contenute in Strutture, come Puntatori (un po' come le funzioni Callback), non sapevo come comportarmi correttamente per il resto, incluso l'eventuale uso con esse della Funzione Extern.

Ho posto la questione nella M.L. ufficiale, dalla quale ho ricevuto per ora una risposta che in parte conferma le mie supposizioni sulla dichiarazione di quelle funzioni nel codice Gambas come Puntatori all'interno di una Struttura ed aggiunge altro precisando gli stretti limiti di tale caso:

" 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":
Codice: c [Seleziona]
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:
Codice: c [Seleziona]
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" ? I suppose in a Structure. EXTERN will not be used ?

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


Best regards
vuott
"


" Declaration is easy:
Codice: gambas [Seleziona]
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
"
« Ultima modifica: 22 Aprile 2014, 01:40:05 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »