[Gambas-user] Extern function api calls (2)

Alex Schaller gambas at ...1077...
Tue Aug 16 10:29:50 CEST 2005


Hello Benoit!

Thanks for the quick answer. The only question I have is:
How do I read again the modified values (integer and strings).

I tried all kinds of combinations with the read command but can't figure this 
out.
This whole concept of pointers is still somewhat abstract to me.

Thanks!!

Alex


> 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




More information about the User mailing list