我正在尝试通过dbus发送两个字符串数组。我正在使用gdbus api,带有gdbus-codegen(。
我已将 dbus 接口定义如下。
<method name="foo">
<arg name="count" direction="out" type="i" />
<arg name="array" direction="out" type="a(ia(s))" />
</method>
我正在尝试通过以下方式创建 GVariant:
GVariantBuilder *builder;
GVariant *variant;
builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));
for (int i = 0; i < count; i++)
{
g_variant_builder_add(builder, "(ia(s))",
options[i].num_supported,
pack_string_array(options[i].num_supported, options[i].supported_values));
}
variant = g_variant_new("a(a(s))", builder);
my_interface_complete_foo(interface, invocation, count, variant);
其中pack_string_array将 C 样式字符串数组打包到变量中。
GVariant* pack_string_array(int num_val, char **val)
{
GVariantBuilder *builder;
GVariant *values;
builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
for (int i = 0; i < num_val; i++)
{
g_message("%s", val[i]);
g_variant_builder_add(builder, "(s)", val[i]);
}
if (num_val == 0)
g_variant_builder_add(builder, "(s)", "NA");
values = g_variant_new("a(s)", builder);
return values;
}
但是,当我测试它时,程序崩溃并显示以下错误:
GLib-CRITICAL **: g_variant_builder_end: assertion 'ensure_valid_builder (builder)' failed
g_variant_get_type: assertion 'value != NULL' failed
如何通过 dbus 发送字符串数组数组?
用于构建器和变体的签名不匹配:
builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));
...
variant = g_variant_new("a(a(s))", builder);
GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));
for (int i = 0; i < count; i++) {
GVariantBuilder* builderStr = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
for (int j = 0; j < nr_of_string; j++) {
g_variant_builder_add(builderStr, "(s)", "Test");
}
g_variant_builder_add(builder, "(ia(s))", 32, **builderStr**);
}
GVariant* retVariant = g_variant_builder_end(builder);