FFI: Pass a Smalltalk String as part of a Structure

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

FFI: Pass a Smalltalk String as part of a Structure

Udo Schneider
Hi,

I defined an FFI Structure using

MyStruc class>>fields
        ^#( #path 'char*) )

After calling define Fields I see that the wrappers are generated.
However I not quite sure how to pass a Smalltalk String in.

Something like

MyStruc new path: '/home/udos/'

does not work.

Any pointers?

CU,

Udo


Reply | Threaded
Open this post in threaded view
|

Re: FFI: Pass a Smalltalk String as part of a Structure

Andreas.Raab
Hi -

You need to create an instance of ExternalData and pass this in. For
example:

setPath: aString
   "Set the path in this structure from a Squeak string"
   | xHandle xData |
   xHandle:= ExternalAddress allocate: aString size+1.
   1 to: aString size do:[:i| xHandle byteAt: i-1 put: (aString byteAt: i)].
   xHandle byteAt: aString size put: 0.
   xData := ExternalData fromHandle: xHandle type: ExternalType char
asPointerType.
   ^self path: xData

Hope this helps,
   - Andreas


Udo Schneider wrote:

> Hi,
>
> I defined an FFI Structure using
>
> MyStruc class>>fields
>     ^#( #path 'char*) )
>
> After calling define Fields I see that the wrappers are generated.
> However I not quite sure how to pass a Smalltalk String in.
>
> Something like
>
> MyStruc new path: '/home/udos/'
>
> does not work.
>
> Any pointers?
>
> CU,
>
> Udo
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: FFI: Pass a Smalltalk String as part of a Structure

Udo Schneider
Andreas,

thanks for the info. Works perfectly.

CU,

Udo


Andreas Raab schrieb:

> Hi -
>
> You need to create an instance of ExternalData and pass this in. For
> example:
>
> setPath: aString
>   "Set the path in this structure from a Squeak string"
>   | xHandle xData |
>   xHandle:= ExternalAddress allocate: aString size+1.
>   1 to: aString size do:[:i| xHandle byteAt: i-1 put: (aString byteAt: i)].
>   xHandle byteAt: aString size put: 0.
>   xData := ExternalData fromHandle: xHandle type: ExternalType char
> asPointerType.
>   ^self path: xData
>
> Hope this helps,
>   - Andreas
>
>
> Udo Schneider wrote:
>> Hi,
>>
>> I defined an FFI Structure using
>>
>> MyStruc class>>fields
>>     ^#( #path 'char*) )
>>
>> After calling define Fields I see that the wrappers are generated.
>> However I not quite sure how to pass a Smalltalk String in.
>>
>> Something like
>>
>> MyStruc new path: '/home/udos/'
>>
>> does not work.
>>
>> Any pointers?
>>
>> CU,
>>
>> Udo
>>
>>
>>
>
>
>