Subject: [ruby-ffi] needing help with using a Callback in a :Varargs parameter list |
From: aaalex |
Date: 3/17/11 4:21 AM |
To: ruby-ffi |
Hello ruby-ffi group, hopefully, some FFI wizard can help me with this rather unusual function definition. I try to access the OPSEC API with Ruby FFI. OPSEC is a collection of C libraries to access CheckPoint firewalls and it is very C indeed. So, I ran into a problem of giving a pointer to a callback function inside a varargs parameter list, but let me explain a little bit. To setup an OPSEC entity the API provides the opsec_init_entity function like this: OpsecEntity *opsec_init_entity (OpsecEnv *env, OpsecEntityType *entity_type, ...); so this resolves to the following RubyFFI definition: module Opsec extend FFI::Library ffi_lib 'opsec' typedef :pointer, :opsec_entity attach_function :opsec_init_entity, [:opsec_env, :pointer, :int, :varargs], :opsec_entity end The varargs is a list of attributes and their values and finally ending with a NULL. The attribute is just an integer, the value depends on the attribute and can be an integer, a CHAR * or a handler. As with FFI you need to give the type for every parameter in a :varargs, invoking this function would look something like this: server = opsec_init_entity opsec_env, objptr, :int, OPSEC_ENTITY_NAME, :string, "lea_server", :int, 0 and it works! Isn't FFI just great? But it is also needed to provide callbacks here. This is how it looks like in C: int LeaStartHandler(OpsecSession *); pClient = opsec_init_entity(pEnv, LEA_CLIENT, OPSEC_SESSION_START_HANDLER, LeaStartHandler, OPSEC_EOL); In Ruby callbacks are Procs or Lambdas, but how do I get them into the function call described here with <callback>? client = opsec_init_entity opsec_env, objptr, :int, OPSEC_SESSION_START_HANDLER, :callback, <callback>, :int, 0 Does anybody know a way to give a pointer to a Proc into this function call? Any hint is appreciated. -aaalex