2017-10-13 4 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 코드는 나를 위해 잘 작동 :

** 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 
:

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

그것은 다음과 같은 인쇄