[Gambas-user] external functions and structs

Benoît Minisini g4mba5 at gmail.com
Thu Nov 29 14:00:34 CET 2018


Le 28/11/2018 à 13:39, gen braga a écrit :
> Hi there,
> 
> I'm trying to use a method from an external library whose signature is 
> as follow
> 
> int __stdcall assemble_vdm( ais_state *state, char *str )
> 
> ais_state is:
> 
> #define SIXBIT_LEN   255
> 
> typedef struct {
>      unsigned char msgid;               //!< Message ID 0-31
>      unsigned int  sequence;            //!< VDM message sequence number
>      unsigned int  total;               //!< Total # of parts for the 
> message
>      unsigned int  num;                 //!< Number of the last part stored
>      char          channel;             //!< AIS Channel character
>      sixbit        six_state;           //!< sixbit parser state
> } ais_state;
> 
> six_state is:
> 
> typedef struct {
>      char bits[SIXBIT_LEN];          //!< raw 6-bit ASCII data string
>      char *p;                        //!< pointer to current character 
> in bits
>      unsigned char remainder;        //!< Remainder bits
>      unsigned char remainder_bits;   //!< Number of remainder bits
> } sixbit;
> 

Your structure has holes and alignment problems.

In that case, the compiler will reorder the fields in memory 
arbitrarily, and the Gambas interpreter does not know how to deal with that.

If you made the library, try to reorder the structure fields so that 
there is no hole nor alignment problems anymore.

In the future, I may be able to associate Gambas structures with libffi 
structures, so that passing Gambas structure to extern functions 
eventually work in all cases. Alas, nothing sure !

Regards,

-- 
Benoît Minisini


More information about the User mailing list