[Pharo-users] Re: uFFI provide buffer to C

2021-11-29 Thread Gerry Weaver
Hi Guillermo,

Ah ok. I didn’t realize that I could only have a single statement. I got it 
working now.

Thanks to you and everyone that helped me out.

Thanks,
Gerry

Gerry Weaver
(800) 318-6118

From: Guillermo Polito 
Sent: Monday, November 29, 2021 3:11 AM
To: Any question about pharo is welcome 
Subject: [Pharo-users] Re: uFFI provide buffer to C

Hi Gerry,

I think the point of Tomaz is that an FFI method can only have a single 
#ffiCall: statement.
In your example, what you can do is to split your method in two methods:

First a method with the mapping (following the idea of Tomaz but using libc)

FFITutorial>>getHostNameInto: hostName ofLenght: length
^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
'libc.so.6'.

And then have a second method that calls this one:

FFITutorial>>getHostName

   | nameBuffer |

nameBuffer := ByteArray new: 256.

self getHostNameInto: nameBuffer ofLenght: 256.

^nameBuffer asString

Notice that converting the byte array buffer to string will only work by chance 
if the called function returns characters encoded in ascii.
In Pharo strings are Unicode characters where each character is a unicode 
code-point.
The best thing is to read the documentation of the library and check the 
encoding of the string that is put in the buffer to decode it accordingly, for 
example from utf8.

G



El 29 nov 2021, a las 0:56, Gerry Weaver 
mailto:ger...@compvia.com>> escribió:

Hi,

Thanks for the example. Unfortunately, it doesn't work on Linux.

Thanks,
Gerry



From: Tomaž Turk mailto:tomaz.t...@ef.uni-lj.si>>
Sent: Sunday, November 28, 2021 11:39 AM
To: Any question about pharo is welcome 
mailto:pharo-users@lists.pharo.org>>
Subject: [Pharo-users] Re: uFFI provide buffer to C

Hi,

try with this:

FFITutorial>>getHostNameInto: hostName ofLenght: length
^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
'Ws2_32.dll'. "... on Windows"

From Playground, you can then do:

| name len |
len := 50.
name:= ByteArray new: len.
FFITutorial new getHostNameInto: name ofLenght: len.
name inspect.

I hope this helps.

Best wishes,
Tomaz



[Pharo-users] Re: uFFI provide buffer to C

2021-11-29 Thread Sven Van Caekenberghe



> On 29 Nov 2021, at 10:10, Guillermo Polito  wrote:
> 
> Hi Gerry,
> 
> I think the point of Tomaz is that an FFI method can only have a single 
> #ffiCall: statement.
> In your example, what you can do is to split your method in two methods:
> 
> First a method with the mapping (following the idea of Tomaz but using libc)
> 
> FFITutorial>>getHostNameInto: hostName ofLenght: length
>> ^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
>> 'libc.so.6'. 
> 
> And then have a second method that calls this one:
> 
> FFITutorial>>getHostName
> 
>| nameBuffer |  
> 
> nameBuffer := ByteArray new: 256. 
> 
> self getHostNameInto: nameBuffer ofLenght: 256.
> 
> ^nameBuffer asString
> 
> Notice that converting the byte array buffer to string will only work by 
> chance if the called function returns characters encoded in ascii.
> In Pharo strings are Unicode characters where each character is a unicode 
> code-point.

In general #utf8Decoded is better than #asString since UTF-8 includes 7-bit 
ASCII. It will fail when the native encoding is one of the simpler byte 
encodings, like Latin1 (or iso-8859-1), though.

> The best thing is to read the documentation of the library and check the 
> encoding of the string that is put in the buffer to decode it accordingly, 
> for example from utf8.
> 
> G
> 
> 
>> El 29 nov 2021, a las 0:56, Gerry Weaver  escribió:
>> 
>> Hi,
>> 
>> Thanks for the example. Unfortunately, it doesn't work on Linux.
>> 
>> Thanks,
>> Gerry
>> 
>> 
>> From: Tomaž Turk 
>> Sent: Sunday, November 28, 2021 11:39 AM
>> To: Any question about pharo is welcome 
>> Subject: [Pharo-users] Re: uFFI provide buffer to C
>>  
>> Hi,
>> 
>> try with this:
>> 
>> FFITutorial>>getHostNameInto: hostName ofLenght: length
>> ^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
>> 'Ws2_32.dll'. "... on Windows"
>> 
>> From Playground, you can then do:
>> 
>> | name len |
>> len := 50.
>> name:= ByteArray new: len.
>> FFITutorial new getHostNameInto: name ofLenght: len. 
>> name inspect.
>> 
>> I hope this helps.
>> 
>> Best wishes,
>> Tomaz
> 


[Pharo-users] Re: uFFI provide buffer to C

2021-11-29 Thread Guillermo Polito
Hi Gerry,

I think the point of Tomaz is that an FFI method can only have a single 
#ffiCall: statement.
In your example, what you can do is to split your method in two methods:

First a method with the mapping (following the idea of Tomaz but using libc)

FFITutorial>>getHostNameInto: hostName ofLenght: length
> ^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
> 'libc.so.6'. 

And then have a second method that calls this one:

FFITutorial>>getHostName

   | nameBuffer |  

nameBuffer := ByteArray new: 256. 

self getHostNameInto: nameBuffer ofLenght: 256.

^nameBuffer asString

Notice that converting the byte array buffer to string will only work by chance 
if the called function returns characters encoded in ascii.
In Pharo strings are Unicode characters where each character is a unicode 
code-point.
The best thing is to read the documentation of the library and check the 
encoding of the string that is put in the buffer to decode it accordingly, for 
example from utf8.

G


> El 29 nov 2021, a las 0:56, Gerry Weaver  escribió:
> 
> Hi,
> 
> Thanks for the example. Unfortunately, it doesn't work on Linux.
> 
> Thanks,
> Gerry
> 
> 
> From: Tomaž Turk 
> Sent: Sunday, November 28, 2021 11:39 AM
> To: Any question about pharo is welcome 
> Subject: [Pharo-users] Re: uFFI provide buffer to C
>  
> Hi,
> 
> try with this:
> 
> FFITutorial>>getHostNameInto: hostName ofLenght: length
> ^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
> 'Ws2_32.dll'. "... on Windows"
> 
> From Playground, you can then do:
> 
> | name len |
> len := 50.
> name:= ByteArray new: len.
> FFITutorial new getHostNameInto: name ofLenght: len. 
> name inspect.
> 
> I hope this helps.
> 
> Best wishes,
> Tomaz



[Pharo-users] Re: uFFI provide buffer to C

2021-11-28 Thread Gerry Weaver
Hi,

Thanks for the example. Unfortunately, it doesn't work on Linux.

Thanks,
Gerry



From: Tomaž Turk 
Sent: Sunday, November 28, 2021 11:39 AM
To: Any question about pharo is welcome 
Subject: [Pharo-users] Re: uFFI provide buffer to C

Hi,

try with this:

FFITutorial>>getHostNameInto: hostName ofLenght: length
^self ffiCall: #( int gethostname(char *hostName, int length) ) library: 
'Ws2_32.dll'. "... on Windows"

>From Playground, you can then do:

| name len |
len := 50.
name:= ByteArray new: len.
FFITutorial new getHostNameInto: name ofLenght: len.
name inspect.

I hope this helps.

Best wishes,
Tomaz



[Pharo-users] Re: uFFI provide buffer to C

2021-11-28 Thread Tomaž Turk

Hi,

try with this:

FFITutorial>>getHostNameInto: hostName ofLenght: length
^self ffiCall: #( int gethostname(char *hostName, int length) ) 
library: 'Ws2_32.dll'. "... on Windows"



From Playground, you can then do:


| name len |
len := 50.
name:= ByteArray new: len.
FFITutorial new getHostNameInto: name ofLenght: len.
name inspect.

I hope this helps.

Best wishes,
Tomaz


[Pharo-users] Re: uFFI provide buffer to C

2021-11-28 Thread Gerry Weaver
Hi,

I'm just trying to work out how to do it. Please find my latest attempt below. 
The docs aren't much help with this.

FFITutorial>>getHostName

   | nameBuffer |

nameBuffer := ByteArray new: 256.

self ffiCall: #( int gethostname(ByteArray *nameBuffer, int 256) ) library: 
'libc.so.6'.

^nameBuffer asString



Thanks,
Gerry




From: Esteban Lorenzano 
Sent: Sunday, November 28, 2021 3:09 AM
To: Any question about pharo is welcome 
Cc: pharo-users@lists.pharo.org 
Subject: [Pharo-users] Re: uFFI provide buffer to C

hi,

it should be ByteArray.
if you explain more in detail your problem, I can try to help you better :)

Esteban

On Nov 28 2021, at 9:20 am, Gerry Weaver  wrote:

Hello All,

Pharo newbie here. I'm playing around with uFFI and I'm having trouble figuring 
out how to provide a buffer to C. It looks like maybe ByteArray would be the 
type, but it's not working. Would some one be so kind as to show me an example 
with the C function gethostname?


Thanks,
Gerry


[Pharo-users] Re: uFFI provide buffer to C

2021-11-28 Thread Esteban Lorenzano
hi,

it should be ByteArray.
if you explain more in detail your problem, I can try to help you better :)

Esteban
On Nov 28 2021, at 9:20 am, Gerry Weaver  wrote:
>
> Hello All,
>
> Pharo newbie here. I'm playing around with uFFI and I'm having trouble 
> figuring out how to provide a buffer to C. It looks like maybe ByteArray 
> would be the type, but it's not working. Would some one be so kind as to show 
> me an example with the C function gethostname?
>
>
> Thanks,
> Gerry
>
>
>