// 建立与系统总线的连接
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (!conn) {
exit(1);
}
// 构造调用"org.example.TestServer"的"HellowWorld"方法的消息
DBusMessage *msg;
msg = dbus_message_new_method_call("org.example.TestServer",
"/org/example/TestObject", "org.example.TestInterface", "HelloWorld");
if (msg == NULL) {
fprintf(stderr, "Message Null\n");
exit(1);
}
// 发送消息并等待回复
DBusMessage *reply;
reply = dbus_connection_send_with_reply_and_block(conn, msg,
-1, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Reply Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (reply == NULL) {
fprintf(stderr, "Reply Null\n");
exit(1);
}
// 读取回复消息的内容
char *result;
dbus_message_get_args(reply, &err,
DBUS_TYPE_STRING, &result, DBUS_TYPE_INVALID);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Arg Error (%s)\n", err.message);
dbus_error_free(&err);
}
printf("Received Reply: %s\n", result);
dbus_message_unref(msg);
dbus_message_unref(reply);
dbus_connection_close(conn);
return 0;
评论