Subject:
Re: [ruby-ffi] basic question: function expects pointer to string buffer, returns string
From:
Stephen Bannasch
Date:
12/12/09 7:08 PM
To:
ruby-ffi@googlegroups.com

>I've started working on a Ruby FFI interface to the Vernier GoIO sensor interface products.
>
>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

Here's a simplification of how I solved the problem:

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)