获取GVariant

时间:2017-10-13 09:30:18

标签: c++ glib dbus gio

我目前尝试与dbus通信并拥有一个将返回array of struct(string, uint32, string, string, object path)的函数。我将结果存储在GVariant中并打印此GVariant表示其中包含正确的结果值。

更具描述性:我尝试获取Systemd的Logind Managers ListSessions的结果。

印刷品的输出是:

[('2', uint32 1000, 'nidhoegger', 'seat0', objectpath
'/org/freedesktop/login1/session/_32'), ('6', 1001, 'test', 'seat0',
'/org/freedesktop/login1/session/_36'), ('c2', 111, 'lightdm',
'seat0', '/org/freedesktop/login1/session/c2')]

我现在正在尝试的是使用以下方法将每个数组元素放入循环中:

for (uint32_t i = 0; i < ::g_variant_n_children(v); ++i)
{
    GVariant *child = ::g_variant_get_child_value(v, i);
}

打印孩子时我得到了:

<('2', uint32 1000, 'nidhoegger', 'seat0', objectpath '/org/freedesktop/login1/session/_32')>

到目前为止一切顺利。现在我试图以这种方式使用g_variant_get来获取单个项目:

gchar *id = NULL;
uint32_t uid = 0;
gchar *user = NULL;
gchar *seat = NULL;
gchar *session_path = NULL;

::g_variant_get(v, "(susso)", &id, &uid, &user, &seat, &session_path);

但它只给了我这个断言:

(process:12712): GLib-CRITICAL **: the GVariant format string '(susso)' has a type of '(susso)' but the given value has a type of 'v'

(process:12712): GLib-CRITICAL **: g_variant_get_va: assertion 'valid_format_string (format_string, !endptr, value)' failed

如果这是相关的:我生成了与gdbus-codegen进行通信的代码,并且获取该值的函数具有以下签名:

gboolean login1_manager_call_list_sessions_sync (
    Login1Manager *proxy,
    GVariant **out_unnamed_arg0,
    GCancellable *cancellable,
    GError **error);

我做错了什么?为什么它将“v”作为值返回?

1 个答案:

答案 0 :(得分:0)

browser.find_element(:xpath, "//button[@class='btn btn-primary' and @type='submit']")

看起来很可疑。您应该在::g_variant_get(v, "(susso)", &id, &uid, &user, &seat, &session_path); 上调用它,而不是child

以下C代码适用于我:

v

它打印以下内容:

/* gcc `pkg-config --cflags --libs glib-2.0` -o test test.c */
#include <glib.h>

int
main (void)
{
  g_autoptr(GVariant) sessions = NULL;

  sessions = g_variant_new_parsed ("[('2', uint32 1000, 'nidhoegger', 'seat0', objectpath '/org/freedesktop/login1/session/_32'), ('6', 1001, 'test', 'seat0', '/org/freedesktop/login1/session/_36'), ('c2', 111, 'lightdm', 'seat0', '/org/freedesktop/login1/session/c2')]");

  for (gsize i = 0; i < g_variant_n_children (sessions); i++)
    {
      g_autoptr(GVariant) child = g_variant_get_child_value (sessions, i);
      g_message ("Child %" G_GSIZE_FORMAT ": %s", i, g_variant_get_type_string (child));

      guint32 uid;
      const gchar *id, *user, *seat, *session_path;

      g_variant_get (child, "(&su&s&s&o)", &id, &uid, &user, &seat, &session_path);

      g_message ("%s, %u, %s, %s, %s", id, uid, user, seat, session_path);
    }

  return 0;
}