【问题标题】:Send a message using D-Bus使用 D-Bus 发送消息
【发布时间】:2018-01-15 00:45:39
【问题描述】:

我想使用 D-Bus 发送消息,但出现错误:

process 30860: arguments to dbus_message_new_signal() were incorrect, assertion "_dbus_check_is_valid_path (path)" failed in file ../../../dbus/dbus-message.c line 1456.
This is normally a bug in some application using the D-Bus library.
Message is null!

我的代码:

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


int main(){
    DBusConnection *conn;
    DBusError err;
    dbus_error_init(&err);
    conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
    if(!conn){
        fprintf(stderr, "DBus error %s: %s\n", err.name, err.message);
        return(1);
    }
    dbus_bus_request_name(conn, "org.test", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
    if(dbus_error_is_set(&err)){
        fprintf(stderr, "DBus error %s: %s\n", err.name, err.message);
        dbus_connection_close(conn);
        return(1);
    }
    DBusMessage *msg;
    msg = dbus_message_new_signal("org/test/mon/data", "org.test.mon.data", "Data");
    if(msg == NULL){
        fprintf(stderr, "Message is null!\n");
        return(1);
    }
    dbus_message_append_args(msg, DBUS_TYPE_STRING, "My message", DBUS_TYPE_INVALID);
    if(!dbus_connection_send(conn, msg, NULL)) fprintf(stderr, "Error sending message!\n");
    dbus_message_unref(msg);
    dbus_connection_flush(conn);
    dbus_connection_close(conn);
}

我尝试学习几十个教程和示例,但我想我错过了一些东西。

我只需要发送一条带有文本的消息。

【问题讨论】:

    标签: c dbus


    【解决方案1】:

    除了按照 jku 的建议修复代码之外,建议你不要使用 libdbus 连接 D-Bus:它的设计已经过时,正确使用很痛苦。

    使用更现代的高级 API 更容易,例如 GDBus

    【讨论】:

    • 完全同意这一点,如果我有选择,我永远不会再使用libdbus。
    【解决方案2】:

    dbus_message_new_signal() 的第一个参数是 D-Bus 路径,规范说明了路径:

    路径必须以 ASCII '/'(整数 47)字符开头

    【讨论】:

    • 谢谢!多么愚蠢的错误……
    猜你喜欢
    • 2013-11-14
    • 2013-01-16
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2017-03-30
    • 2015-07-10
    • 2013-05-20
    相关资源
    最近更新 更多