开发者

How do I specify a struct as the return value of a function in RubyFFI?

开发者 https://www.devze.com 2023-02-18 19:55 出处:网络
I have to load several functions that return structures from the library itself. attach_function \'fn_name\', [], # ... What do I put here?

I have to load several functions that return structures from the library itself.

attach_function 'fn_name', [], # ... What do I put here?

RubyFFI's Wiki pages seem to be outdated, so I'm a little lost here.

How do I create a FFI::Struct, and how do I specify it as the return type of开发者_运维百科 a native function?


class SOME_STRUCT < FFI::Struct 
    layout :a, :float, 
           :b, :float
end

and then

attach_function 'fn_name', [], SOME_STRUCT

and if it stack-allocated struct:

typedef struct
{ 
    float a, b; 
} SOME_STRUCT;

you should use this:

attach_function 'fn_name', [], SOME_STRUCT.by_value
0

精彩评论

暂无评论...
验证码 换一张
取 消