[Gambas-user] Extern function api calls

Benoit Minisini gambas at ...1...
Mon Aug 15 20:51:56 CEST 2005


On Monday 15 August 2005 01:41, Alex Schaller wrote:
> Hello!
>
> Does anyone have more info on how to use the extern() function in Gambas.
> Possibly also on ho to use it with passing structures. I need to create a
> structure and then pass its address to a C library which fills some
> variables of the structure with data.
> I have also tried the gb.api component but when I followed the examples it
> did not seem to work.
>
> I am new to C and Gambas so please be gentle.
>
> Thanks!!!!
>
> Alex
>

This is not well documented yet. Did you search in http://new.gambasdoc.org ?

In a few word, you have to:

1) Allocate the structure with Alloc(Size) - You must know the size of the
structure. You get a Pointer that you must keep in a variable.

2) You use WRITE by using the previous pointer as stream to write binary data 
into the allocated structure.

3) Finally you use the pointer to pass the structure to the extern call.

Example:

The C structure is:

struct foo {
  int a;
  char *b;
  double c;
  }

You do:

DIM pPtr AS Pointer
DIM a AS Integer
DIM b AS String
DIM pBPtr AS Pointeer
DIM c AS Float

' Allocate the string
pBPtr = Alloc(b)
pPtr = Alloc(16)
WRITE #pPtr, a
WRITE #(pPtr + 4), pBPtr
WRITE #(pPtr + 8), c

' Then you can use pPtr with a C extern function argument whose type is 
'struct foo *'

Free(pPtr)
Fre(pBPtr)

I hope this was clear...

Regards,


-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list