How to wrap function in Ruby FFI method that takes struct as argument?

不羁岁月 提交于 2019-12-06 06:45:36

Check how to use Structs in https://github.com/ffi/ffi/wiki/Structs, for your case:

class What < FFI::Struct
  layout :d, :int,
         :something, :pointer
end

Now attach the function, the argument, since you are passing the struct by value, is going to be What.by_value(replacing What by whatever you have named you struct class above):

attach_function 'doit', [What.by_value],:int

And now how to call the function:

mywhat = DoitLib::What.new
mywhat[:d] = 1234
DoitLib.doit(mywhat)

And now the complete file:

require 'ffi'

module DoitLib
  extend FFI::Library
  ffi_lib "path/to/yourlibrary.so"

  class What < FFI::Struct
    layout :d, :int,
           :something, :pointer
  end

  attach_function 'doit', [What.by_value],:int

end

mywhat = DoitLib::What.new
mywhat[:d] = 1234
DoitLib.doit(mywhat)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!