我想使用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);
}


我尝试遵循许多教程和示例,但我想我错过了一些东西。

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

最佳答案

dbus_message_new_signal()的第一个参数是D-Bus路径,规格说明了有关路径的信息:


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

关于c - 使用D-Bus发送消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45551516/

10-12 18:04