1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Owning bus names</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="GIO Reference Manual">
8 <link rel="up" href="ch31.html" title="Migrating to GDBus">
9 <link rel="prev" href="ch31s02.html" title="API comparison">
10 <link rel="next" href="ch31s04.html" title="Creating proxies for well-known names">
11 <meta name="generator" content="GTK-Doc V1.18 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="ch31s02.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="ch31.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GIO Reference Manual</th>
20 <td><a accesskey="n" href="ch31s04.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="idp76275952"></a>Owning bus names</h2></div></div></div>
26 Using dbus-glib, you typically call RequestName manually
27 to own a name, like in the following excerpt:
29 <div class="informalexample"><pre class="programlisting">
31 res = dbus_g_proxy_call (system_bus_proxy,
34 G_TYPE_STRING, NAME_TO_CLAIM,
35 G_TYPE_UINT, DBUS_NAME_FLAG_ALLOW_REPLACEMENT,
37 G_TYPE_UINT, &result,
43 g_warning ("Failed to acquire %s: %s",
44 NAME_TO_CLAIM, error->message);
49 g_warning ("Failed to acquire %s", NAME_TO_CLAIM);
54 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
58 g_warning ("Failed to acquire %s: %s",
59 NAME_TO_CLAIM, error->message);
64 g_warning ("Failed to acquire %s", NAME_TO_CLAIM);
69 dbus_g_proxy_add_signal (system_bus_proxy, "NameLost",
70 G_TYPE_STRING, G_TYPE_INVALID);
71 dbus_g_proxy_connect_signal (system_bus_proxy, "NameLost",
72 G_CALLBACK (on_name_lost), NULL, NULL);
74 /* further setup ... */
80 While you can do things this way with GDBus too, using
81 <a class="link" href="GDBusProxy.html#g-dbus-proxy-call-sync" title="g_dbus_proxy_call_sync ()"><code class="function">g_dbus_proxy_call_sync()</code></a>, it is much nicer to use the high-level API
84 <div class="informalexample"><pre class="programlisting">
86 on_name_acquired (GDBusConnection *connection,
90 /* further setup ... */
95 owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
97 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
104 g_main_loop_run (loop);
106 g_bus_unown_name (owner_id);
110 Note that <a class="link" href="gio-Owning-Bus-Names.html#g-bus-own-name" title="g_bus_own_name ()"><code class="function">g_bus_own_name()</code></a> works asynchronously and requires
111 you to enter your mainloop to await the <code class="function">on_name_aquired()</code>
112 callback. Also note that in order to avoid race conditions (e.g.
113 when your service is activated by a method call), you have to export
114 your manager object <span class="emphasis"><em>before</em></span> acquiring the
115 name. The <code class="function">on_bus_acquired()</code> callback is the right place to do
121 Generated by GTK-Doc V1.18</div>