D-Bus returns UnkownMethod - 说路径上的对象没有这样的接口

D-Bus returns UnkownMethod - saying No such interface on object at path

我想写一个简短的程序,它会触发递归地为我的网络共享中的所有图片创建缩略图(例如,一夜之间)- 这样当我访问 Thunar 中的文件夹时,缩略图会立即显示。

我会说,当我可以通过 gdbus call 访问 API 时,系统设置正确:

$ gdbus call --session --dest org.freedesktop.thumbnails.Thumbnailer1 \
             --object-path /org/freedesktop/thumbnails/Thumbnailer1 \
             --method org.freedesktop.thumbnails.Thumbnailer1.GetSchedulers
(['default', 'foreground', 'background'],)
$

但是当我调用二进制文件时,出现以下错误:

$ cc -Wall -Werror -pthread -I/usr/include/libmount -I/usr/include \
/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
-c -o main.o main.c
$ cc main.o -lgio-2.0 -lgobject-2.0 -lglib-2.0 -o tumbler-bin
$ ./tumbler-bin 
**
ERROR:main.c:26:main: assertion failed (error == NULL):
GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface
?org.freedesktop.thumbnails.Thumbnailer1? on object at path \
/org/freedesktop/thumbnails/Thumbnailer1 (g-dbus-error-quark, 19)
Bail out! ERROR:main.c:26:main: assertion failed (error == NULL):
GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface
?org.freedesktop.thumbnails.Thumbnailer1? on object at path \
/org/freedesktop/thumbnails/Thumbnailer1 (g-dbus-error-quark, 19)
Aborted (core dumped)
$

代码如下。一般来说,我为 org.freedesktop.thumbnails.Thumbnailer1 创建一个代理并调用 GetSchedulers():

#include <stdio.h>
#include <gio/gio.h>

const char *THUMBNAILER_IFACE   = "org.freedesktop.thumbnails.Thumbnailer1";
const char *THUMBNAILER_SERVICE = "/org/freedesktop/thumbnails/Thumbnailer1";
const char *THIS_NAME = "de.test.tumbler-bin";

int main() {

    GError *error = NULL;

    GDBusConnection *con = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
    g_assert_no_error(error);
    g_assert(con != NULL);

    guint name_id = g_bus_own_name_on_connection(con, THIS_NAME, 
                    G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
    g_assert(name_id != 0);

    GDBusProxy *proxy = g_dbus_proxy_new_sync(con, G_DBUS_PROXY_FLAGS_NONE,
                        NULL, THIS_NAME, THUMBNAILER_SERVICE, THUMBNAILER_IFACE,
                        NULL, &error);
    g_assert_no_error(error);
    if (proxy == NULL) printf("proxy is NULL\n");
    g_assert(proxy != NULL);

    GVariant *schedulers = g_dbus_proxy_call_sync(proxy, "GetSchedulers",
                            NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
    g_assert_no_error(error);
    if (schedulers == NULL) printf("schedulers is NULL\n");
    g_assert(schedulers != NULL);
    
    return 0;
}

稍后我想调用方法 Queue() - 但为了测试 D-Bus 是否正常工作,我使用 GetSchedulers().

您检查过 dbus-monitor 发生了什么吗?我不熟悉 gdbus 的工作原理,但看起来你正在尝试给自己打电话:

guint name_id = g_bus_own_name_on_connection(con, THIS_NAME, 
                    G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
// ...
GDBusProxy *proxy = g_dbus_proxy_new_sync(con, G_DBUS_PROXY_FLAGS_NONE,
                        NULL, THIS_NAME, THUMBNAILER_SERVICE, THUMBNAILER_IFACE,
                        NULL, &error);

既然你想在不同的服务上调用该方法,看起来你真正想要的是:

const char *THUMBNAILER_IFACE   = "org.freedesktop.thumbnails.Thumbnailer1";
const char *THUMBNAILER_PATH    = "/org/freedesktop/thumbnails/Thumbnailer1";
const char *THUMBNAILER_DEST    = "org.freedesktop.thumbnails.Thumbnailer1";
const char *THIS_NAME = "de.test.tumbler-bin";
// ...
    GDBusProxy *proxy = g_dbus_proxy_new_sync(con, G_DBUS_PROXY_FLAGS_NONE,
                        NULL, THUMBNAILER_DEST, THUMBNAILER_PATH, THUMBNAILER_IFACE,
                        NULL, &error);

有关方法参数,请参阅 g_dbus_proxy_new_sync 的文档。