Subject:
Re: [ruby-ffi] Pointer to a struct - how to cast?
From:
DA
Date:
5/20/10 1:00 PM
To:
ruby-ffi@googlegroups.com

On Thu, May 20, 2010 at 1:19 PM, Jeremy Voorhis <jvoorhis@gmail.com> wrote:
> The address is in the output of inspect (0x4145ec0). You can also use
> Pointer#to_i.

Thanks Jeremy, so I'm getting some weird numbers for the addresses,
which makes think they're not valid.  I have an array pointers to
these structs:
for p in parr
  puts "p: #{p} inspect: #{p.inspect}"
  i+= 1
  break if i > 10
  if p.to_i > 10
    my_struct = FM::FTextItemT.new p
    puts "test: #{my_struct[:dataType]}"
  end
end

I'm getting:
p: #<FFI::Pointer:0x317a04> inspect: #<FFI::Pointer address=00000001>
p: #<FFI::Pointer:0x3179dc> inspect: #<FFI::Pointer address=00000001>
p: #<FFI::Pointer:0x3179b4> inspect: #<FFI::Pointer address=003F7330>
test: 7566450
p: #<FFI::Pointer:0x31798c> inspect: #<FFI::Pointer address=00000008>
p: #<FFI::Pointer:0x317964> inspect: #<FFI::Pointer address=00000004>
p: #<FFI::Pointer:0x3452c4> inspect: #<FFI::Pointer address=00000000>
p: #<FFI::Pointer:0x317928> inspect: #<FFI::Pointer address=00000001>
p: #<FFI::Pointer:0x317900> inspect: #<FFI::Pointer address=00000001>
p: #<FFI::Pointer:0x3178d8> inspect: #<FFI::Pointer address=003F7368>
test: 1634615657
p: #<FFI::Pointer:0x3178b0> inspect: #<FFI::Pointer address=0000000F>

Here's how I'm getting the array:

// c struct:
typedef struct {
	UIntT len;
	F_TextItemT *val;
} F_TextItemsT;

// ruby def:
class FTextItemsT < FFI::Struct
    layout :len, :uint,
      :val, :pointer
 end

// ruby code:

tis = FM.FApiGetText(docid, flowid, (FM.FTIString | FM.FTILineEnd));
parr = tis[:val].read_array_of_pointer(tis[:len])
puts "tis len: #{tis[:len]}"

The tis[:len] gives me the same value as calling this code in c, so
I'm pretty sure the FApiGetText call is working and I'm getting a
valid struct returned. Am I using read_array_of_pointer wrong?

Thanks,
Drew