Retrieving an Array of Strings, that is attached to a G_HASH_TABLE variant

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Jun 6 05:02:29 PDT 2008


On Thu, 05 Jun 2008 at 23:42:00 -0500, David Stockwell wrote:
> (GStrv) gPropValue = (GStrv) g_hash_table_lookup(propTable, "Strings"); // Assume that the "key" is "Strings"

This is wrong - an a{sv} maps to a GHashTable from gchar * to GValue *.

The right way to access it is like this:

GValue *tmp = g_hash_table_lookup (propTable, "Strings");
gchar **gPropValue;

if (G_VALUE_HOLDS (value, G_TYPE_STRV))
  {
    gPropValue = g_value_get_boxed (tmp);
  }
else
  {
    /* caller has done the wrong thing - recover in whatever way you
     * think best (but crashing in response to D-Bus messages is never the
     * right answer) */
    g_debug ("wtf? someone's using the wrong type - ignoring this message");
    return;
  }

Similarly, if you have a (string|integer|...) value in your dictionary,
it'll actually be a GValue containing a (string|integer|...), and you
have to check with (G_VALUE_HOLDS_STRING|G_VALUE_HOLDS_INT|...) that the
caller gave you the right thing, then call
(g_value_get_string|g_value_get_int|...) to get the actual value.

The functions tp_asv_get_uint32 (etc.) in telepathy-glib/dbus.[ch],
available from <http://telepathy.freedesktop.org/releases/telepathy-glib/> or
<http://darcs.collabora.co.uk/>, illustrate how to use an a{sv} in
dbus-glib code.

Regards,
    Simon


More information about the dbus mailing list