From: Takaaki Tateishi Date: 2004-12-09T23:45:04+09:00 Subject: Re: Function pointer with Ruby/DL Guillaume Marcais wrote: > How can I call a pointer to a function. Let say: > > > int my_function(void); > int (*function_hook)(void); > function_hook = my_function; > 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