From: Daniel Berger Date: 2009-08-08T01:45:49+09:00 Subject: Re: FFI, sysctl, pointer question Andrea Fazzi wrote: > Daniel Berger wrote: >> Hi, >> >> I'm trying to get better at FFI, but I'm getting stumped on how to >> create and pass pointers to functions. >> >> In the example below, in the Sys::Uptime.seconds method, I'm trying to >> figure out how to create and pass a pointer for the mib (a 2-element >> integer array) and the timeval size (i.e. where I have a '?' instead of >> actual code). >> >> What should the actual code be? > > You could explicity instantiate two MemoryPointer objects initialized > with > the proper values: > > mib_ptr = FFI::MemoryPointer.new(:int, 2).write_array_of_int(mib) > tv_size_ptr = FFI::MemoryPointer.new(:int).write_int(tv.size) > > Then you could pass those values to sysctl function: > > sysctl(mib_ptr, 2, tv, tv_size_ptr, nil, 0) Thank you, that worked. > To ask for more (and better) help feel free to post your questions to > the Ruby-FFI ml at users@ruby-ffi.kenai.com If we want FFI to gain traction among Rubyists (we do, don't we?), it's better to ask & answer here IMO. Regards, Dan