Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Last active April 5, 2019 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr33n7007h/af8d422e7c4a0e0a3742985fec6448ed to your computer and use it in GitHub Desktop.
Save gr33n7007h/af8d422e7c4a0e0a3742985fec6448ed to your computer and use it in GitHub Desktop.
Testing a C callback from Ruby
#include <stdio.h>
// compile: gcc -g -fPIC -Wall -Werror -Wextra -pedantic callback.c -shared -o libtest.so
void
message(void)
{
puts("wassupppp!");
}
void
cb_func(void (*ptr)())
{
(*ptr)();
}
void
call(void)
{
void (*ptr)() = &message;
cb_func(ptr);
}
>> handle = Handle.new './libtest.so'
=> #<Fiddle::Handle:0x000055e0dce08680>
>>
>> fn = Function.new(handle['call'], [], TYPE_VOID)
=> #<Fiddle::Function:0x000055e0dce6aec0 @abi=2, @args=[], @ptr=140541166813510, @return_type=0>
>>
>> fn.()
wassupppp!
=> nil
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment