我花了四个小时深入搜索:),但没有找到:How to get PID from DBUS sender under Qt。
从其他 dbus 库来看,它非常简单:
Call dbus_message_get_sender to get the caller's D-Bus id, and then call GetConnectionUnixProcessID to get the pid of the process that sent the message.
如何从 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() );
}
最佳答案
1) 常量 QDBusMessage & QDBusContext::message () 常量 返回生成此调用的消息。
2) QString QDBusMessage::service () const 返回服务名称或远程方法调用的总线地址。
3) QDBusReply QDBusConnectionInterface::servicePid (const QString & serviceName) const 返回当前持有总线服务 serviceName 的进程的 Unix 进程 ID (PID)。
关于c++ - 从 DBUS 获取发送者 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785610/