Marshalling bytewise data with Glib bindings

Matt Hoosier matt.hoosier at gmail.com
Mon May 21 08:30:35 PDT 2007


On 5/21/07, Matt Hoosier <matt.hoosier at gmail.com> wrote:
> On 5/21/07, Thiago Macieira <thiago at kde.org> wrote:
> > Matt Hoosier wrote:
> > >Is there a suitable datatype to use as the contents of a GValue, which
> > >the dbus-glib bindings will understand natively? Shoehorning this
> > >stuff into GValue whose type is G_TYPE_STRING sounds bad; the first 0
> > >byte will cause the bindings to think that the "string" is finished,
> > >and UTF-8 enforcement will probably choke on this method anyway.
> >
> > ARRAY of BYTE should do it.
> >
> > STRING should be used only for properly-encoded UTF-8 data. NULs are
> > allowed, but arbitrary binary data isn't.
>
> Sorry; I should have been more clear. I'm attempting to send a
> dictionary style data structure across D-Bus as an "a{sv}". What Glib
> datatype stuck inside a GValue (e.g., a boxed GArray or similar) will
> be automatically recognized as a legal instance of the "v" D-Bus type?

I found that the following works, with the non-obvious key being to
fetch the D-Bus GType for the GArray over whichever primitive type you
need:

        GArray * byte_array = g_array_sized_new (FALSE,
                                                 FALSE,
                                                 sizeof (guint8),
                                                 size);
        byte_array = g_array_append_vals (byte_array, data, size);

        GValue * v = (GValue *) g_malloc (sizeof (GValue));
        memset (v, 0, sizeof (GValue));
        g_value_init (v, dbus_g_type_get_collection ("GArray", G_TYPE_UCHAR));
        g_value_set_boxed_take_ownership (v, byte_array);


More information about the dbus mailing list