如何在以 struct 作为参数的 Ruby FFI 方法中包装函数?
我正在尝试使用 ruby-ffi 从共享对象调用函数。我将以下内容编译成共享对象:
#include <stdio.h>
typedef struct _WHAT {
int d;
void * something;
} WHAT;
int doit(WHAT w) {
printf("%d\n", w.d);
return w.d;
}
问题是,如何在 Ruby 中使用 attach_function
声明该函数? Ruby 中的参数列表中的结构参数 (WHAT w) 是如何定义的?它不是一个 :pointer,并且似乎不适合 ruby-ffi 文档中描述的任何其他可用类型,那么它会是什么?
I am trying to call a function from a shared object using ruby-ffi. I compiled the following into a shared object:
#include <stdio.h>
typedef struct _WHAT {
int d;
void * something;
} WHAT;
int doit(WHAT w) {
printf("%d\n", w.d);
return w.d;
}
The problem is, how do I declare the function with attach_function
in Ruby? How is the struct argument (WHAT w) defined in the list of arguments in Ruby? It is not a :pointer, and does not seem to fit any of the other available types described in the ruby-ffi documentation, so what would it be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 https://github.com/ffi/ 中查看如何使用 Structs ffi/wiki/Structs,对于您的情况:
现在附加函数,参数,因为您按值传递结构,将是
What.by_value
(用上面您命名的结构类替换 What):现在如何调用函数:
现在是完整的文件:
Check how to use Structs in https://github.com/ffi/ffi/wiki/Structs, for your case:
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):And now how to call the function:
And now the complete file: