c++ - 从 DBUS 获取发送者 PID

标签 c++ qt dbus

我花了四个小时深入搜索:),但没有找到: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/

相关文章:

c++ - 如何调整 FindBoost.cmake 静态库搜索 x32 boost 库?

qt - QMake:范围如何工作?

linux - C 中的 D-Bus 教程与 wpa_supplicant 通信

c++ - 在 Visual Studio 2015 中使用和导出 std::string 和 std::vector<std::string>

c++ - 如何获取从命令行参数调用的文件的目录?

c++ - 编写完一个游戏后,如何绑定(bind)可执行文件中的 Assets 并打包,以便分发给其他人?

c++ - QTcpServer 应用程序中 Qt C++ 中的槽和信号

c++ - 用 C++ 设计 Qt Creator 的侧边栏

python - 声明在 DBus 上订阅了哪些信号?

c++ - 使用 dbus-cpp 列出 WPA 请求者网络属性