Subject: Re: [ruby-ffi] Low-level dbus API, ffi and strings |
From: Wayne Meissner |
Date: 2/10/10 4:30 AM |
To: ruby-ffi@googlegroups.com |
On 10 February 2010 20:20, Christoph Kappel <unexist@dorfelite.net> wrote:
Hello, I have some problems calling following C function with a string as value: dbus_bool_t dbus_message_iter_append_basic(DBusMessageIter *iter, int type, const void *value) I attached it via ffi: attach_function(:dbus_message_iter_append_basic, :dbus_message_iter_append_basic, [ :pointer, DBusType, :pointer ], :bool ) (DBusType is an enum with the allowed types as integer.) In C it should look like this and it allows several types casted to void: DBusMessageIter iter; char *value = "foobar"; dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &value);
The ruby-ffi equivalent is: value = FFI::MemoryPointer.from_string("subtle") ptr = FFI::MemoryPointer.new(:pointer) ptr.put_pointer(0, value) dbus_message_iter_append_basic(piter, :type_string, ptr) i.e. you need to allocate a chunk of native memory to hold the string, then allocate another bit of native memory to hold the address of that first native chunk, and pass that in.