0
我正在開發一個小型項目。我的操作系統是Linux(Ubuntu 11.04)。我想連接到DBus守護進程信號NameOwnerChanged以指示哪個應用程序正在關閉或啓動。我寫了一個小程序來執行此操作。它雖然失敗了。我只用了glib dbus,沒有dbus低級別。無法連接基於DBusGProxy的dbus信號
這裏是我的代碼:
/的gcc -o測試的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
,它仍然會失敗。爲什麼?