我正在开发一个小项目。我的操作系统是Linux(Ubuntu 11.04)。我想连接到DBus守护进程信号NameOwnerChanged,以便指出哪个应用程序已关闭或启动。我写了一个小程序来执行此操作。但它失败了。我只使用了glib dbus,没有dbus低级别。
这是我的代码:
/ gcc -o test main.c pkg-config --libs --cflags glib-2.0 dbus-1 dbus-glib-1
/
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib-lowlevel.h>
#define D_SERVICE "org.freedesktop.DBus"
#define D_PATH "/"
#define D_INTERFACE "org.freedesktop.DBus"
GMainLoop* loop;
DBusGConnection* conn;
DBusGProxy* d_proxy;
static void dbus_name_owner_changed(DBusGProxy *proxy, char* name, char* old, char* new, gpointer user_data)
{
g_print("%s owner change \n", name);
}
int main(int argc,char** argv)
{
g_type_init();
loop = g_main_loop_new(NULL, FALSE);
conn = dbus_g_bus_get(DBUS_BUS_SESSION,NULL);
d_proxy = dbus_g_proxy_new_for_name(conn, D_SERVICE, D_PATH, D_INTERFACE);
guint ret;
GError * error = NULL;
dbus_g_proxy_call( d_proxy, "RequestName", &error,
G_TYPE_STRING, "com.asianux.test",
G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
G_TYPE_INVALID,
G_TYPE_UINT, &ret,
G_TYPE_INVALID);
if(error==NULL && ret==DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
{
g_print("Request Name Success \n");
}
else
{
if(error)
{
g_print("Request Name Failed %s\n", error->message);
}
}
dbus_g_proxy_add_signal(d_proxy,"NameOwnerChanged",
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal(d_proxy,"NameOwnerChanged",
G_CALLBACK(dbus_name_owner_changed),
NULL,NULL);
g_main_loop_run(loop);
}
我编译并运行它,但函数dbus_name_owner_changed从未运行过。为什么我不能成功连接信号?
我知道在添加信号并连接信号之前我错过了dbus_g_object_register_marshaller
。但是,即使我在代码中添加dbus_g_object_register_marshaller
,它仍然会失败。为什么呢?
答案 0 :(得分:0)
我已经解决了。因为D_PATH应该是“/ org / freedesktop / DBus”。