main.cpp
文件内容:
#include <dbus/dbus.h> #include <cstddef> #include <cstdio> #include <iostream> int main(int argc, char *argv[]) { (void)argc; (void)argv; DBusError dbus_error; DBusConnection *dbus_conn = nullptr; DBusMessage *dbus_msg = nullptr; DBusMessage *dbus_reply = nullptr; const char *dbus_result = nullptr; // Initialize D-Bus error ::dbus_error_init(&dbus_error); // Connect to D-Bus if (nullptr == (dbus_conn = ::dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error))) { ::perror(dbus_error.name); ::perror(dbus_error.message); // Compose remote procedure call } else if (nullptr == (dbus_msg = ::dbus_message_new_method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus.Introspectable", "Introspect"))) { ::dbus_connection_unref(dbus_conn); ::perror("ERROR: ::dbus_message_new_method_call - Unable to allocate memory for the message!"); // Invoke remote procedure call, block for response } else if (nullptr == (dbus_reply = ::dbus_connection_send_with_reply_and_block(dbus_conn, dbus_msg, DBUS_TIMEOUT_USE_DEFAULT, &dbus_error))) { ::dbus_message_unref(dbus_msg); ::dbus_connection_unref(dbus_conn); ::perror(dbus_error.name); ::perror(dbus_error.message); // Parse response } else if (!::dbus_message_get_args(dbus_reply, &dbus_error, DBUS_TYPE_STRING, &dbus_result, DBUS_TYPE_INVALID)) { ::dbus_message_unref(dbus_msg); ::dbus_message_unref(dbus_reply); ::dbus_connection_unref(dbus_conn); ::perror(dbus_error.name); ::perror(dbus_error.message); // Work with the results of the remote procedure call } else { std::cout << "Connected to D-Bus as \"" << ::dbus_bus_get_unique_name(dbus_conn) << "\"." << std::endl; std::cout << "Introspection Result:" << std::endl; std::cout << std::endl << dbus_result << std::endl << std::endl; ::dbus_message_unref(dbus_msg); ::dbus_message_unref(dbus_reply); /* * Applications must not close shared connections - * see dbus_connection_close() docs. This is a bug in the application. */ //::dbus_connection_close(dbus_conn); // When using the System Bus, unreference // the connection instead of closing it ::dbus_connection_unref(dbus_conn); } return 0; }点击复制复制失败已复制
编写 Makefile
:
LDFLAGS += -lhv -ldbus-1 SRCS_DIR := $(wildcard ./*.cpp) all: clean bin/test bin/test: mkdir -p bin $(CXX) $(CFLAGS) $(SRCS_DIR) -o $@ $(LDFLAGS) clean: rm -rf bin .PHONY: clean 点击复制复制失败已复制
编译并运行:
$ make && ./bin/test点击复制复制失败已复制
得到内容:
Connected to D-Bus as ":1.218". Introspection Result: <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <node> <interface name="org.freedesktop.DBus"> <method name="Hello"> <arg direction="out" type="s"/> </method> <method name="RequestName"> <arg direction="in" type="s"/> <arg direction="in" type="u"/> <arg direction="out" type="u"/> </method> <method name="ReleaseName"> <arg direction="in" type="s"/> <arg direction="out" type="u"/> </method> <method name="StartServiceByName"> <arg direction="in" type="s"/> <arg direction="in" type="u"/> <arg direction="out" type="u"/> </method> <method name="UpdateActivationEnvironment"> <arg direction="in" type="a{ss}"/> </method> <method name="NameHasOwner"> <arg direction="in" type="s"/> <arg direction="out" type="b"/> </method> <method name="ListNames"> <arg direction="out" type="as"/> </method> <method name="ListActivatableNames"> <arg direction="out" type="as"/> </method> <method name="AddMatch"> <arg direction="in" type="s"/> </method> <method name="RemoveMatch"> <arg direction="in" type="s"/> </method> <method name="GetNameOwner"> <arg direction="in" type="s"/> <arg direction="out" type="s"/> </method> <method name="ListQueuedOwners"> <arg direction="in" type="s"/> <arg direction="out" type="as"/> </method> <method name="GetConnectionUnixUser"> <arg direction="in" type="s"/> <arg direction="out" type="u"/> </method> <method name="GetConnectionUnixProcessID"> <arg direction="in" type="s"/> <arg direction="out" type="u"/> </method> <method name="GetAdtAuditSessionData"> <arg direction="in" type="s"/> <arg direction="out" type="ay"/> </method> <method name="GetConnectionSELinuxSecurityContext"> <arg direction="in" type="s"/> <arg direction="out" type="ay"/> </method> <method name="GetConnectionAppArmorSecurityContext"> <arg direction="in" type="s"/> <arg direction="out" type="s"/> </method> <method name="ReloadConfig"> </method> <method name="GetId"> <arg direction="out" type="s"/> </method> <method name="GetConnectionCredentials"> <arg direction="in" type="s"/> <arg direction="out" type="a{sv}"/> </method> <property name="Features" type="as" access="read"> <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/> </property> <property name="Interfaces" type="as" access="read"> <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/> </property> <signal name="NameOwnerChanged"> <arg type="s"/> <arg type="s"/> <arg type="s"/> </signal> <signal name="NameLost"> <arg type="s"/> </signal> <signal name="NameAcquired"> <arg type="s"/> </signal> </interface> <interface name="org.freedesktop.DBus.Introspectable"> <method name="Introspect"> <arg direction="out" type="s"/> </method> </interface> <interface name="org.freedesktop.DBus.Peer"> <method name="GetMachineId"> <arg direction="out" type="s"/> </method> <method name="Ping"> </method> </interface> <node name="org/freedesktop/DBus"/> </node>