Subject: Re: [ruby-ffi] basic question: function expects pointer to string buffer, returns string |
From: Shawn Anderson |
Date: 12/12/09 10:02 PM |
To: ruby-ffi@googlegroups.com |
>I've started working on a Ruby FFI interface to the Vernier GoIO sensor interface products.Here's a simplification of how I solved the problem:
>
>See:
>
> http://www.vernier.com/go/
> http://www.vernier.com/downloads/gosdk.html *the SDK is public domain
>
>I've got a very basic question FFI question.
>
>I have a C++ function that expects a pointer to a string buffer and a int32 specifying the size of the buffer.
>
>The result of calling the function is a name of a connected device in the string buffer.
>
>How should the call be setup and then processed on return to generate a Ruby string.
>
>Here's the state of the code so far: http://gist.github.com/254981
I create a MemoryPointer pointing to a 256 byte buffer:
name = FFI::MemoryPointer.new(256)
then call my function (also telling the function to limit the returned string to 255 chars):
GetDeviceName(name, 255)
and print the returned string:
puts name.get_string(0)