简体   繁体   English

GLib - 获取通过 D-Bus 返回的数组中的字符串数

[英]GLib - getting the number of strings in array returned over D-Bus

I am getting an array of strings via a function call over D-Bus as follows.我通过 D-Bus 上的 function 调用获得了一个字符串数组,如下所示。 I am using GLib then to parse the returned values.然后我使用 GLib 来解析返回值。 The code below is greatly simplified.下面的代码大大简化了。 I can read each string returned using g_variant_iter_loop and the iterator .我可以读取使用g_variant_iter_loopiterator返回的每个字符串。

How can I first determine how many strings have been returned in the array?我怎样才能首先确定数组中返回了多少个字符串?

I need to allocate memory dynamically first depending on how many strings have been returned, before reading the actual strings.在读取实际字符串之前,我需要首先根据返回的字符串数量动态分配 memory。 This may seem simple, but i've been looking online for the best way to do this without success.这可能看起来很简单,但我一直在网上寻找最好的方法来做到这一点,但没有成功。 Thanks in advance.提前致谢。

GVariant * val_returned = g_dbus_connection_call_sync(connection,
                          SERVICE,
                          OBJECT,
                          IFACE,
                          "GetStringsArray",
                          NULL,
                          G_VARIANT_TYPE("(as)"),
                          G_DBUS_CALL_FLAGS_NONE,
                         -1,
                          NULL,
                          &err);

g_autoptr(GVariantIter) iterator = NULL;
g_variant_get(val_returned, "(as)", &iterator);

// Need to determine how many strings have been read into the array
// Use g_variant_iter_loop to retrieve each string as a gchar*

Call g_variant_iter_n_children (iterator) .调用g_variant_iter_n_children (iterator)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM