B
Ben Giddings
I just started using Ruby/DL and am amazed at what it can do. I've
almost got a really impressive demo to show off to my cow-orkers, but I
have one cow left to ork... er... I have one thing left to figure out.
Given some C code like this:
typdef struct {
int status;
int results[16];
} foo_t;
int get_results(handle_t the_handle, foo_t *results_ptr)
{
results_ptr->status = 0;
results_ptr->results[0] = 0xFF;
return 0;
}
How do I interface to that code in Ruby/DL?
I already have functions that take handles, I just treat it as a pointer
and everything works great:
require 'dl/import'
module LIBMYAPI
extend DL::Importable
dlload "./libmyapi.so.1.0.1"
typealias("size_t", "unsigned int")
typealias("id_t", "int")
extern "void *open_handle(char *)"
extern "char *get_version(void *)"
extern "int close_handle(void *)"
end
if __FILE__ == $0
hostname = 'foo'
handle = LIBMYAPI.open_handle("m4-c0050c")
result = LIBMYAPI.get_version(handle)
puts result
LIBM4API.close_handle(handle)
end
I just don't know how to tell Ruby/DL that get_results takes a pointer,
then changes it.
While I'm at it, what's the proper way to deal with a structure
containing an array, if you want to access things by name?
Something like this works for the 'timeval' struct, but I don't know how
to use it with structs containing arrays.
ptr = DL.malloc(DL.sizeof('LL'))
ptr.struct!('LL', :tv_sec, :tv_usec)
ptr[:tv_sec] = 10
ptr[:tv_usec] = 100
sec = ptr[:tv_sec]
usec = ptr[:tv_usec]
Thanks!
Ben
almost got a really impressive demo to show off to my cow-orkers, but I
have one cow left to ork... er... I have one thing left to figure out.
Given some C code like this:
typdef struct {
int status;
int results[16];
} foo_t;
int get_results(handle_t the_handle, foo_t *results_ptr)
{
results_ptr->status = 0;
results_ptr->results[0] = 0xFF;
return 0;
}
How do I interface to that code in Ruby/DL?
I already have functions that take handles, I just treat it as a pointer
and everything works great:
require 'dl/import'
module LIBMYAPI
extend DL::Importable
dlload "./libmyapi.so.1.0.1"
typealias("size_t", "unsigned int")
typealias("id_t", "int")
extern "void *open_handle(char *)"
extern "char *get_version(void *)"
extern "int close_handle(void *)"
end
if __FILE__ == $0
hostname = 'foo'
handle = LIBMYAPI.open_handle("m4-c0050c")
result = LIBMYAPI.get_version(handle)
puts result
LIBM4API.close_handle(handle)
end
I just don't know how to tell Ruby/DL that get_results takes a pointer,
then changes it.
While I'm at it, what's the proper way to deal with a structure
containing an array, if you want to access things by name?
Something like this works for the 'timeval' struct, but I don't know how
to use it with structs containing arrays.
ptr = DL.malloc(DL.sizeof('LL'))
ptr.struct!('LL', :tv_sec, :tv_usec)
ptr[:tv_sec] = 10
ptr[:tv_usec] = 100
sec = ptr[:tv_sec]
usec = ptr[:tv_usec]
Thanks!
Ben