How to pass an image using dbus glib-binding

Sumit Kumar Jain sumitskj_20 at
Fri Feb 29 01:26:07 PST 2008

I want to pass an image file which is around 10K using dbus glib bindings. I tried to find about it and the only data type that seems to suite my need is GArray. However debugging through the code, i found out that the actual message size is four times the size of my image file. That i think is because dbus maps GArray to DBUS_TYPE_ARRAY. GArray has a pointer of gchar * while DBUS_TYPE_ARRAY is an array of uint32. I guess that the reason of the message size increasing by 4 times as 3 NULL bytes are appended to every byte of the GArray. Now it seems that that GArray is not useful and i must use some other data type. Any suggestion as to which data type might work. Also some example code demonstrating the usage will be gr8. 

The following code demonstrates the case : 

------- client code ------
int main (int argc, char **argv)
    DBusGConnection *connection;
    GError *error;
    DBusGProxy *proxy;
    GArray *array;
    guint length;
    FILE *fp = NULL;
    int len;

    g_type_init ();

    error = NULL;
    connection = dbus_g_bus_get (DBUS_BUS_SESSION,
    if (connection == NULL)
      g_printerr ("Failed to open connection to bus: %s\n",
      g_error_free (error);
      exit (1);

    proxy = dbus_g_proxy_new_for_name (connection,

    error = NULL;

        printf("Error --- %s\n",error->message);
        return 1;

    printf("File details... length = %d\n",length);
    error = NULL;

    fp = fopen("copy-client","wb");
    len = fwrite(array->data,4,array->len,fp);

    return 0;

---------- End Client code ------------

--------- Server code -------------
#ifndef __SOME_OBJECT_H__
#define __SOME_OBJECT_H__

#include <glib-object.h>

typedef struct _SomeObject SomeObject;
struct _SomeObject
    GObject        parent_obj;
    gint         m_a;
    gchar*        m_b;
    gfloat        m_c;

typedef struct _SomeObjectClass SomeObjectClass;
struct _SomeObjectClass
    GObjectClass    parent_class;

    /* Some useful methods may follow. */
    gboolean    (*method1)    (SomeObject *self, gint x,GArray **y,gint *z,GError **error);
    void        (*method2)    (SomeObject *self, gchar*);

GType    some_object_get_type ();

gboolean some_object_method1 (SomeObject *self, gint x,GArray **y,gint *z,GError **);    /* virtual */
void    some_object_method2 (SomeObject *self, gchar*);    /* virtual */
void    some_object_method3 (SomeObject *self, gfloat);    /* non-virtual */

/* Handy macros */
#define SOME_OBJECT_TYPE        (some_object_get_type ())
#define SOME_OBJECT(obj)        (G_TYPE_CHECK_INSTANCE_CAST ((obj), SOME_OBJECT_TYPE, SomeObject))
#define SOME_OBJECT_CLASS(c)        (G_TYPE_CHECK_CLASS_CAST ((c), SOME_OBJECT_TYPE, SomeObjectClass))
#define SOME_IS_OBJECT(obj)        (G_TYPE_CHECK_TYPE ((obj), SOME_OBJECT_TYPE))

// End some-object.h

/// some-object.c
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "some-object.h"
#include "some-object-glue.h"
#include <dbus/dbus-glib-bindings.h>
#include <errno.h>

static GObjectClass *parent_class = ((void *)0);
static void some_object_init (SomeObject *self);

char *FILENAME = "test.jpeg";
int SIZE = 0;

gboolean some_object_method1_impl (SomeObject *self, gint a, GArray **y,gint *z,GError **error)
    struct stat stat_buf;
    gchar *ptr = NULL;
    FILE *fp = NULL;
    int err;
    unsigned int length = 0;
    self->m_a = a;
    g_print ("Method1: %i\n", self->m_a);
    *y = g_array_new(FALSE,FALSE,1);

    SIZE = stat_buf.st_size;
    ptr = g_malloc(SIZE);
    errno = 0;
    fp = fopen(FILENAME,"rb");
    err = errno;
        FILE *fp2 = fopen("c:\\copy-server","wb");
        *z = SIZE;    
    return TRUE;

void some_object_method2_impl (SomeObject *self, gchar* b)
    self->m_b = b;
    g_print ("Method2: %s\n", self->m_b);

/* Public methods. */
gboolean some_object_method1 (SomeObject *self, gint a,GArray **y,gint *z,GError **error)
    return SOME_OBJECT_GET_CLASS (self)->method1 (self, a,y,z,error);

void    some_object_method2 (SomeObject *self, gchar* b)
    SOME_OBJECT_GET_CLASS (self)->method2 (self, b);

void    some_object_method3 (SomeObject *self, gfloat c)
    self->m_c = c;
    g_print ("Method3: %f\n", self->m_c);

void    some_object_dispose (GObject *self)
    static gboolean first_run = TRUE;

    if (first_run)
        first_run = FALSE;
        /* Call g_object_unref on any GObjects that we hold, but don't break the object */

        parent_class-> dispose (self);

void    some_object_finalize (GObject *self)
    parent_class-> finalize (self);

/* Here is where we override any functions. Since we have no properties or even fields, none of the below are needed. */
void    some_object_class_init        (gpointer g_class, gpointer class_data)
    GObjectClass    *object_class    = G_OBJECT_CLASS (g_class);
    SomeObjectClass    *this_class    = SOME_OBJECT_CLASS (g_class);
    //assign value to parent class
    parent_class = g_type_class_peek_parent (g_class);
    //assing pointer values to the base class members
    object_class-> dispose = &some_object_dispose;
    object_class-> finalize = &some_object_finalize;
    //assign value to derived class members
    this_class->method1 = &some_object_method1_impl;
    this_class->method2 = &some_object_method2_impl;


void some_object_init (SomeObject *self)
    self->m_a = 1;
    self->m_c = 1.03f;
    self->m_b = "sumit";

GType some_object_get_type () 
    static GType g_define_type_id = 0; 
    if ((g_define_type_id == 0)) 
        static const GTypeInfo g_define_type_info = 
            sizeof (SomeObjectClass), 
            (GBaseInitFunc) ((void *)0), 
            (GBaseFinalizeFunc) ((void *)0), 
            (GClassInitFunc) some_object_class_init, 
            (GClassFinalizeFunc) ((void *)0), 
            ((void *)0), 
            sizeof (SomeObject), 
            (GInstanceInitFunc) some_object_init, 

        g_define_type_id = g_type_register_static 
            (GTypeFlags) 0

    return g_define_type_id; 

int main(int argc,char *argv[])
    SomeObject *so = NULL;
    DBusGConnection *bus;
    GMainLoop *mainLoop = NULL;
    unsigned int request_ret;
    GError *error = NULL;

    DBusGProxy *proxy = NULL;
    char *x;

    so = g_object_new(SOME_OBJECT_TYPE,NULL);

    bus = dbus_g_bus_get(DBUS_BUS_SESSION,NULL);

    proxy = dbus_g_proxy_new_for_name(bus,DBUS_SERVICE_DBUS,DBUS_PATH_DBUS,DBUS_INTERFACE_DBUS);


        g_print("Unable to register service\n");
        return 1;

    mainLoop = g_main_loop_new(NULL,FALSE);
    return 0;

----- End Server code ----


      Get the freedom to save as many mails as you wish. To know how, go to
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the dbus mailing list