如何在 C 中使用 gio/glib 列出 dbus 服务下的所有对象路径?

How to list all object paths under a dbus service, in C with gio/glib?

对此 question 的跟进,但我想用 C 来做,而不是 python,使用 glib-2.0/gio-2.0。我真的很难在 C 中找到这样的例子,而且文档对于新手来说很难阅读,因为它只是一个巨大的 api 列表。

对于客户端 DBus 调用,您可以在 glib 中使用 GDbusProxy 对象。 在原始问题中使用 org.freedesktop.DBus.Introspectable:

int
main (int argc, char *argv[])
{
  GError *error;
  GDBusProxyFlags flags;
  GDBusProxy *proxy;
  gpointer data;

  loop = g_main_loop_new (NULL, FALSE);

  error = NULL;
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM
                                     flags,
                                     NULL, /* GDBusInterfaceInfo */
                                     name, /* your service name */
                                     object_path, /* your root object */
                                   "org.freedesktop.DBus.Introspectable",
                                     NULL, /* GCancellable */
                                     &error);
g_dbus_proxy_call(proxy,
                 "Introspect", NULL,
                  G_DBUS_CALL_FLAGS_NONE,
                 -1, NULL,
                  (GAsyncReadyCallback) some_callback,
                  &data);

然后你可以定义函数some_callback来处理包含对象的xml。