Search code examples
cdbus

libdbus reading array type result


I have to read org.freedesktop.NetworkManager.GetDevices

My program calls the method and receives result correctly.

The problem is how to support array of string/paths type? How get length of the array and how to obtain single element, Thanks.


Solution

  • For an array of not fixed-size elements (such as strings), we must call dbus_message_iter_recurse(&args, &string);, giving a pointer to the main iterator as args and pointer for new iterator as the second argument.

    To obtain element, we call dbus_message_iter_get_basic(&string, &paths);, where paths is a pointer to a character array.

    The whole code looks like below:

    if (!dbus_message_iter_init(msg, &args))
      fprintf(stderr, "Message has no arguments!\n");
    
      do {
        dbus_message_iter_recurse(&args, &string);
        do {
    
          dbus_message_iter_get_basic(&string, &paths);
          puts(paths);
        }
        while (dbus_message_iter_next(&string));
      } while (dbus_message_iter_next(&args));