Function pointer with Ruby/DL

Yet another question about this amazing library.

How can I call a pointer to a function. Let say:

<code>
int my_function(void);
int (*function_hook)(void);
function_hook = my_function;
</code>

Can I call the function pointed to by function_hook?

Guillaume.

Guillaume Marcais wrote:

How can I call a pointer to a function. Let say:

<code>
int my_function(void);
int (*function_hook)(void);
function_hook = my_function;
</code>

If the function_hook is globally defined variable, I mean it should be exported, the address
of function_hook is obtained as follows.

module MyLib
   extend DL::Importable
   dlload "foo.so"

   HOOK_ADDR = symbol "function_hook"
end

and use Symbol.new to construct function call as follows:

def call_function_hook()
   sym = DL::Symbol.new(MyLib::HOOK_ADDR.ptr, "function_hook", "I")
   result = sym.call
   return result[0]
end

the following foo.c and foo.rb are samples..

foo.c --
int my_function(){
   return 20;
}

int (*function_hook)();

void define_hook(){
   function_hook = &my_function;
}

foo.rb --
require 'dl/import'

module MyLib
   extend DL::Importable
   dlload "foo.so"

   extern "void define_hook()"

   HOOK_ADDR = symbol "function_hook"
   def call_function_hook()
     sym = DL::Symbol.new(HOOK_ADDR.ptr, "function_hook", "I")
     result = sym.call
     result[0]
   end
   module_function :call_function_hook
end

#MyLib.define_hook()
p MyLib.call_function_hook()

ยทยทยท

--
Takaaki Tateishi <ttate@ttsky.net>