2017-10-13 21 views
0

私は現在dbusと通信しようとしており、その機能はarray of struct(string, uint32, string, string, object path)です。 GVariantに結果が格納されていて、このGVariantを印刷すると正しい結果が表示されます。GVariantの内容を取得

さらに詳しい説明:私はSystemdのログインマネージャー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」を返すのでしょうか?

答えて

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

これは疑わしいです。 vではなく、childに電話する必要があります。

次のCコードは、私のために正常に動作します:

/* 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; 
} 

それは次のように出力されます

** Message: Child 0: (susso) 
** Message: 2, 1000, nidhoegger, seat0, /org/freedesktop/login1/session/_32 
** Message: Child 1: (susso) 
** Message: 6, 1001, test, seat0, /org/freedesktop/login1/session/_36 
** Message: Child 2: (susso) 
** Message: c2, 111, lightdm, seat0, /org/freedesktop/login1/session/c2