[Gambas-user] Passing Pointers

Benoit Minisini gambas at ...1...
Wed Mar 21 19:48:30 CET 2007


On vendredi 16 mars 2007, Vince Scott (SBC) wrote:
> I have an Extern 'C' method that I am calling from a shared library that I
> have created. I need to pass in a char* and I am having issues figuring out
> how to do this successfully. I figured out that if I initial the String
> that I am passing from Gambas by using the Space method (kind of like you
> do in Visual Basic when you pass a string to a 'C' method) that I can pass
> a String and have it return a value. BUT this is limiting. What I need to
> do is pass in a string and have the 'C' method allocate the memory space.
> Has anyone done this or have any ideas how this can be accomplished?
>
>
>
> Thanks,
>
> Vince Scott
>

To pass a string to a extern 'C' function, there are two ways:

1) If the string will not be modified by the 'C' function, then you can use 
the Gambas String datatype safely. Logically, the corresponding 'C' argument 
must be declared as 'const char *'.

2) If the string will be modified by the 'C' function, then you cannot use the 
String datatype. You must use the Pointer datatype, and allocate the space by 
using the Alloc() function. Don't forget to free it with Free() when you have 
done with it.

Please give the full 'C' function declaration so that I can be more precise. 
You can read the wiki documentation about external function management too.

Regards,

-- 
Benoit Minisini




More information about the User mailing list