Subject:
[ruby-ffi] Re: Low-level dbus API, ffi and strings
From:
Christoph Kappel
Date:
2/10/10 4:54 AM
To:
ruby-ffi



On Feb 10, 11:30 am, Wayne Meissner <wmeiss...@gmail.com> wrote:
> On 10 February 2010 20:20, Christoph Kappel <unex...@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.

Hello Wayne,

thanks for the fast and working answer. I thought it must be like
that, but couldn't think
of way to archieve it.