我试图使用gdbus通过dbus发送不同的数据类型。我无法发送以下数据类型:a{ias}。有人给我一个片段或任何方法来解决这个问题吗?

最佳答案

我这里有一个片段,只需在下面的代码中替换busname、path、interface和methodname。

  GDBusProxy *proxy;
  GDBusConnection *connection;
  GError *error;
  GVariantBuilder* builder;
  error = NULL;
 **//Acquire bus connection**
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM,
                           NULL,
                           &error);
  g_assert_no_error (error);
  error = NULL;

**//Create proxy of remote object**
proxy= g_dbus_proxy_new_sync(connection,
                             G_DBUS_PROXY_FLAGS_NONE,
                             NULL,              /* GDBusInterfaceInfo */
                            "org.busname",      /* Services */
                            "/org/buspath",     /* Path  */
                            "org.interface",    /* Interface */
                            NULL,               /* GCancellable */
error);

GVariant *result;
GVariant *value;
GError *error;
error = NULL;
  int i;
g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);

**//Sending dictionary datatype start**

  for (i = 0; i < 2; i++)
    {
     GVariantBuilder* builderAs = g_variant_builder_new(G_VARIANT_TYPE("as"));
          for (int j = 0; j < 2; j++) {
              g_variant_builder_add(builderAs, "s", "SomeString");
      }
      g_variant_builder_add(&builder, "{ias}",i,builderAs);
    }

**//Sending dictionary end**

GVariant *v1 = g_variant_builder_end(&builder);

result = g_dbus_proxy_call_sync(proxy,
                "MethodName",
                g_variant_new_tuple(&v1, 1),
                G_DBUS_CALL_FLAGS_NONE,
                -1,
                NULL,
                &error);

10-07 15:22