我试图将一个G_Variant指针返回到一个字典,该字典将uint16作为键并将其映射到字节数组(即:" a {qay}")。
这是我的尝试:
range
当我尝试在D-Bus上查询此属性时,它没有工作,我收到以下错误消息:
#define KEY 0xDEAD
#define BYTE1 0xBE
#define BYTE2 0xEF
GVariantBuilder *arrBuilder, *builder;
GVariant *arr;
//build array of bytes ('ay')
arrBuilder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
g_variant_builder_add(arrBuilder, "y", BYTE1);
g_variant_builder_add(arrBuilder, "y", BYTE2);
arr = g_variant_new("v",g_variant_new("ay", arrBuilder));
//put it in a dict ('a{sv}')
builder = g_variant_builder_new(G_VARIANT_TYPE("a{qay}"));
g_variant_builder_add(builder, "{qay}", KEY, arr);
return g_variant_builder_end(builder);
答案 0 :(得分:0)
arr = g_variant_new("v",g_variant_new("ay", arrBuilder));
也许你应该结束数组构建器,以便它返回你的bytearray?
arr = g_variant_builder_end(arrBuilder);
编辑:我找到了答案:
以下代码将实现您想要做的事情,诀窍是字节数组是变体(一切都是变体)!
GVariant *arr_builder, *dict_builder;
/* Build the array of bytes */
arr_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
g_variant_builder_add(arr_builder, "y",0xAA);
// Add some more if you want
GVariant *byte_array = g_variant_builder_end(arr_builder);
g_variant_builder_unref(arr_builder);
/* Build the dictionary, now is the trick */
dict_builder = g_variant_builder_new(G_VARIANT_TYPE("a{qv}"));
g_variant_builder_add(dict_builder, "{qv}", 0x0F0F, byte_array);
GVariant *dict = g_variant_builder_end(dict_builder);
g_variant_builder_unref(dict_builder);
/* Now you can print your variants (dict and byte_array) and print their type */
g_print(g_variant_print(dict,TRUE));
g_print(g_variant_get_type_string(dict));
我在尝试你所写的内容时遇到的错误和你做的工作相同=)