I I have a C struct with a char*
struct result { char* time }
I define an ExternalStructure in FFI , with one field of type char* (note that the syntax char [5] is not accepted ! ).
defineFields generates the accessors time and time:
If I use time: ’05:45’
I get an error. How do I set a string value in an ExternalStructure ?
Annick
On 29.10.2014, at 06:59, Annick Fron list@afceurope.com wrote:
I I have a C struct with a char*
struct result { char* time }
I define an ExternalStructure in FFI , with one field of type char* (note that the syntax char [5] is not accepted ! ).
defineFields generates the accessors time and time:
If I use time: ’05:45’
I get an error. How do I set a string value in an ExternalStructure ?
Annick
Just like in C. You need to create a pointer to a char array and store that. Which means you need to allocate some bytes (ExternalAddress allocate: byteSize + 1), copy your string there (byteAt:put:), add a terminating 0 byte, then store the address of that in your struct. Be sure to free the memory allocated when you're done.
I think there used to be a helper function for that but I can't find it (only found fromCString).
- Bert -
Hi Annick,
you can't pass smalltalk strings or objects to ffi as parameters, you have to allocate the buffer member of your structure on the external heap ( plus to avoid being gc'd before ffi call) with ExternalAddress allocate: <your buffer/string size +1 for string null termination> fill your buffer with byteAt:put: method, and release this memory with free when you have finished.
not sure, but you could try something like:
s := '05:45' copyWith: (Character value: 0). a := ExternalAddress allocate: s size. s asByteArray withIndexDo: [ :b :i | a byteAt: i put: b ]. myStruct time: a. ... call your ffi ... a free
Regards,
Alain
Le 29/10/2014 11:59, Annick Fron a écrit :
I I have a C struct with a char*
struct result { char* time }
I define an ExternalStructure in FFI , with one field of type char* (note that the syntax char [5] is not accepted ! ).
defineFields generates the accessors time and time:
If I use time: ’05:45’
I get an error. How do I set a string value in an ExternalStructure ?
Annick
squeak-dev@lists.squeakfoundation.org