[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Function pointer with Ruby/DL

Guillaume Marcais

12/9/2004 3:21:00 AM

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.



2 Answers

Takaaki Tateishi

12/9/2004 2:45:00 PM

0

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>


MRS. CLEAN aka MRS. ROPER

8/19/2011 10:32:00 PM

0

> For a real education try reading a book!  I suggest you start with "The
> Cancer Cure that Worked" by Barry Lynes.

ROFLMAO

So, you're a conspiracy NUT!

Hilarious!