如何指定一个结构作为RubyFFI中函数的返回值? [英] How do I specify a struct as the return value of a function in RubyFFI?

查看:101
本文介绍了如何指定一个结构作为RubyFFI中函数的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  attach_function'fn_name',[],# ...我在这里放什么? 

RubyFFI的Wiki页面似乎已经过时,所以我在这里有点迷路。



如何创建 FFI :: Struct ,以及如何将其指定为本机函数的返回类型?

解决方案

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

然后

  attach_function'fn_name',[],SOME_STRUCT 

,如果它是堆栈分配的结构体:

  typedef struct 
{
float a,b;
} SOME_STRUCT;

您应该使用这个:

  attach_function'fn_name',[],SOME_STRUCT.by_value 


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

这篇关于如何指定一个结构作为RubyFFI中函数的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆