Subject: [ruby-ffi] Re: Array of Ints |
From: John Lee |
Date: 10/26/11 5:03 PM |
To: ruby-ffi |
Do you know if it is possible to go the other way around? i.e., array of ints as an output parameter So far, I have attach_function :function2, [:pointer], :int ... ptr = FFI::MemoryPointer.new(:int, 10) MyLibrary.function2(ptr) puts ptr.read_array_of_int(10) But all it prints out is [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. Thanks On Oct 26, 12:46 pm, John Lee <jiujitsu1...@gmail.com> wrote:
<cremes.devl...@mac.com> wrote:Thanks a lot. Your code really helped! On Oct 26, 9:35 am, Chuck RemesOn Oct 26, 2011, at 10:31 AM, John Lee wrote:
I tried wrapping my code inside a module, but the problem still persists.
Do you think :pointer is the correct type to represent the array in attach_function.
I know that if you have an array in a struct, you can specify it as: class Struct1 < ::FFI::Struct layout :value1, [:int, 3] end
So I tried to replace :pointer with [:int, 3] in attach_function, but it complains about "unable to resolve type ':int, 3]' <TypeError>" attach_function :function1, [[:int, 3], :int, :int], :int
Has anyone got arrays to work with FFI?
I have gotten it working in my own project. Take a look at my definition for a function that takes an array:
https://github.com/chuckremes/ffi-rzmq/blob/master/lib/ffi-rzmq/libzm...
I call that function here:
https://github.com/chuckremes/ffi-rzmq/blob/master/lib/ffi-rzmq/poll....
The array that is passed to that function is constructed in this file:
https://github.com/chuckremes/ffi-rzmq/blob/master/lib/ffi-rzmq/poll_...
In that file, the #clean method is where most of the array work is done where I construct the array.
I know it's a big example, but it works and has worked without change since the ffi gem was at 0.6.3.
cr