GLib DBus vs Python DBus

October 31st, 2009 Comments Off

I’ve been experimenting with DBus a bit lately for some side projects im doing (more on that soon). Part of the project involves spawning a service via dbus. In python this is trivial and easily discoverable with a google query or two:


import dbus
bus = dbus.SessionBus()
bus.start_service_by_name('org.gnome.Rygel1') # org.gnome.Rygel1 = service name

Ridiculously easy, right? In C using the glib dbus library things are much more difficult. A bit of googling yielded almost nothing when searching for “start service by name c glib” or variants thereof. So I did the evil thing, I checked out the python source. start_service_by_name is turns out, is a wrapper to a normal proxy call to the org.freedesktop.dbus interface function StartServiceByName:


return (True, self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
BUS_DAEMON_IFACE,
'StartServiceByName',
'su', (bus_name, flags)))

Alright… so now how do we do this in C? We could fiddle with dbus_g_proxy_call for a while, or find out that Maemo actually gives us the answer.


DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);

DBusGProxy *proxy = dbus_g_proxy_new_for_name (connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS);
int result;
dbus_g_proxy_call (proxy, "StartServiceByName", &error,
G_TYPE_STRING,
"SERVICE_NAME",
G_TYPE_UINT, 0,
G_TYPE_INVALID,
G_TYPE_UINT, &result,
G_TYPE_INVALID))

Simple, right?