【发布时间】:2012-03-20 11:09:34
【问题描述】:
我花了四个小时深入搜索:),但没有找到:如何在 Qt 下从 DBUS 发送方获取 PID。
从其他 dbus 库来看,这很简单:
调用 dbus_message_get_sender 获取调用者的 D-Bus id,然后调用 GetConnectionUnixProcessID 获取发送消息的进程的 pid。
如何从 Qt 做到这一点? 我尝试使用此代码,但它返回的是我的 PID 而不是客户端应用程序。
class ServerAdaptor: public QDBusAbstractAdaptor
{
public Q_SLOTS:
// this method shared through the D-BUS and called from different application (client)
bool Show(const QString &in0)
{
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, "org.freedesktop.DBus");
if( connection.isConnected() )
{
qDebug() << "Sender PID " << connection.interface()->servicePid( QDBusConnection::sender().baseService() ).value();
}
}
}
编辑:
如何通过 D-BUS (Qt) 从远程应用程序获取 PID
// this class receive D-BUS methods
class Server: public QObject, protected QDBusContext {
public slots:
bool SomeMethod( const QString &name )
{
qDebug() << "PID is: " << connection().interface()->servicePid( message().service() );
}
}
【问题讨论】: