linux下根据进程pid获取对应的window id的方法

有时候我们需要在一个图形化进程中抓取另外一个进程的窗口以嵌入到当前进程的界面里。比如,在QT开发中,在当前的进程界面里启动另外一个进程并将其窗口嵌入到当前进程的界面内。下面的代码示例了如何根据进程的id获取其对应的UI窗口ID(基于x11框架)。

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
#include <list>
#include <stdlib.h>
#include <QMainWindow>

class WindowsMatchingPid
{
public:
    WindowsMatchingPid(Display *display, Window wRoot, unsigned long pid)
        : _display(display)
    {
        _pid = pid;
        // Get the PID property atom.
        _atomPID = XInternAtom(display, "_NET_WM_PID", True);
        if(_atomPID == None)
        {
            std::cout << "No such atom" << std::endl;
            return;
        }

        search(wRoot);
    }

    const std::list<Window> &result() const { return _result; }
    bool hasBadWinId = false;
private:
    unsigned long  _pid = 0;
    Atom           _atomPID;
    Display       *_display = NULL;
    std::list<Window>   _result;
    void
    search(Window w)
    {
        // Get the PID for the current Window.
        Atom           type;
        int            format;
        unsigned long  nItems;
        unsigned long  bytesAfter;
        unsigned char *propPID = 0;
//        printf("win id: %x\n",w);
        if(Success == XGetWindowProperty(
                    _display,
                    w,
                    _atomPID,
                    0,
                    1,
                    False,
                    XA_CARDINAL,
                    &type,
                    &format,
                    &nItems,
                    &bytesAfter,
                    &propPID))
        {
            //printf("got one,pid:%d\n", propPID);
            if(propPID != 0)
            {
                // If the PID matches, add this window to the result set.
                if(_pid == *((unsigned long *)propPID))
                {
                    XWindowAttributes attr;
                    XGetWindowAttributes(_display, w, &attr);
                    printf("mapstate:%d\n", attr.map_state);
                    if(attr.map_state == 2)
                    _result.push_back(w);
                    else
                        hasBadWinId = true;
                }

                XFree(propPID);
            }
        }

        // Recurse into child windows.
        Window    wRoot;
        Window    wParent;
        Window   *wChild;
        unsigned  nChildren;
        if(0 != XQueryTree(_display, w, &wRoot, &wParent, &wChild, &nChildren))
        {
            for(unsigned i = 0; i < nChildren; i++)
                search(wChild[i]);
        }

        //XFree(propPID);
    }

};


unsigned long
get_win_id_from_pid(unsigned long pid, int* hasBad)
{
//    std::cout << "Searching for windows associated with PID == " << pid << std::endl;

    // Start with the root window.
    Display *display = XOpenDisplay(0);
    printf("display--------,----------pid----------%lld\n",pid);

    WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);

    // Print the result.
    const std::list<Window> &result = match.result();
    //std::cout << "list size ===== " << result.size() << std::endl;
    long win_id = 0;
    for(std::list<Window>::const_iterator pos = result.begin(); pos != result.end(); pos++)
    {
        std::cout << "Window #" << (unsigned long)(*pos) << std::endl;
        if((win_id == 0)||(win_id != 0 && (unsigned long)win_id > (unsigned long)(*pos)))
        win_id = (unsigned long)(*pos);
    }
    XCloseDisplay(display);
    if(match.hasBadWinId)
        *hasBad = 1;
    return win_id;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中,可以通过进程ID获取进程名。Linux系统中的每个进程都有一个唯一的进程IDPID),可以使用这个PID获取进程名。 方法一:通过/proc文件系统获取进程名 在Linux系统的/proc目录下,有一个以进程ID命名的文件夹,进入该文件夹后,可以读取该文件夹下的status文件,其中包含了很多与进程相关的信息,包括进程名。可以使用cat命令或者读取文件的方式来获取进程名,例如: ```shell cat /proc/PID/status | grep Name ``` 其中,PID进程的实际进程ID,Name是进程名。 方法二:使用ps命令获取进程名 可以使用ps命令来获取进程的一些信息,其中包括进程名。可以使用以下命令来获取指定进程ID对应进程名: ```shell ps -p PID -o comm= ``` 其中,PID进程的实际进程ID,comm表示进程名。 在使用以上方法获取进程名时,需要注意以下几点: 1. 只有在当前用户有足够权限的情况下,才能够获取其他用户创建的进程进程名。 2. 这些方法都是通过读取系统文件或者执行系统命令来获取进程名的,因此可能会对系统性能产生一定的影响,尤其是对于大量进程的系统。 3. 在获取进程名之前,需要确保该进程仍然存在,否则无法获取进程名。 综上所述,可以通过上述两种方法获取Linux系统中任意进程进程名,从而实现根据进程ID获取进程名的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值