GUI_获取窗口的进程PID
2012-08-23 14:54
405 查看
Ø 功能
l 获取窗口所在进程的PID
Ø 知识点
l 得到默认的display指针:XOpenDisplay
l 通过字串取对应的AtomID:XInternAtom
l 获取窗的某一属性:XGetWindowProperty
Ø 示例
l 源码
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <string.h>
#define MAX_PROPERTY_VALUE_LEN 4096 //取得属性值最大长度
int main(int argc, char **argv)
{
Display *disp = XOpenDisplay(NULL); //获取默认的display指针
Window win = NULL;
int ret = -1;
Atom xa_prop_name;
Atom xa_ret_type;
int ret_format;
unsigned long ret_nitems;
unsigned long ret_bytes_after;
unsigned long tmp_size;
unsigned char *ret_prop;
int id;
if (argc < 2)
{
printf("please input windowID, such as: ./main 0x240001e (use command xwininfo)\n");
return -1;
}
ret = sscanf(argv[1], "0x%0x", &win); // 从程序参数中获取xwindow id
if (ret < 1)
{
printf("please input windowID, such as: ./main 0x240001e (use command xwininfo)\n");
return -1;
}
printf("xwindow id: 0x%0x\n", win);
xa_prop_name = XInternAtom(disp, "_NET_WM_PID", False); // 取对应字串的AtomID
if (XGetWindowProperty(disp, win, xa_prop_name, 0, // 获取窗口属性
MAX_PROPERTY_VALUE_LEN / 4,
False, XA_CARDINAL, &xa_ret_type, // XA_CARDINAL为数值类型
&ret_format, &ret_nitems, &ret_bytes_after,
&ret_prop) != Success) // 后五个参数是返回值
{
printf("Cannot get %s property.\n", "_NET_WM_PID");
return NULL;
}
else
{
memcpy(&id, ret_prop, 4);// 类型传换
printf("window pid: %d\n", id);
}
}
l 编译$ g++ main.cpp -o main ` pkg-config glib-2.0 --libs --cflags` -lX11
l 执行结果
u 在一个终端打开一个带窗口界面的程序
u 使用以下命令获得窗口的xwindow id
$ xwininfo |grep “Window id”
运行命令后用鼠标点击程序窗口
u 运行我们编写的程序,并将十六进程的xwindow id作为参数传给程序
$ ./main 0x240001e
l 获取窗口所在进程的PID
Ø 知识点
l 得到默认的display指针:XOpenDisplay
l 通过字串取对应的AtomID:XInternAtom
l 获取窗的某一属性:XGetWindowProperty
Ø 示例
l 源码
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <string.h>
#define MAX_PROPERTY_VALUE_LEN 4096 //取得属性值最大长度
int main(int argc, char **argv)
{
Display *disp = XOpenDisplay(NULL); //获取默认的display指针
Window win = NULL;
int ret = -1;
Atom xa_prop_name;
Atom xa_ret_type;
int ret_format;
unsigned long ret_nitems;
unsigned long ret_bytes_after;
unsigned long tmp_size;
unsigned char *ret_prop;
int id;
if (argc < 2)
{
printf("please input windowID, such as: ./main 0x240001e (use command xwininfo)\n");
return -1;
}
ret = sscanf(argv[1], "0x%0x", &win); // 从程序参数中获取xwindow id
if (ret < 1)
{
printf("please input windowID, such as: ./main 0x240001e (use command xwininfo)\n");
return -1;
}
printf("xwindow id: 0x%0x\n", win);
xa_prop_name = XInternAtom(disp, "_NET_WM_PID", False); // 取对应字串的AtomID
if (XGetWindowProperty(disp, win, xa_prop_name, 0, // 获取窗口属性
MAX_PROPERTY_VALUE_LEN / 4,
False, XA_CARDINAL, &xa_ret_type, // XA_CARDINAL为数值类型
&ret_format, &ret_nitems, &ret_bytes_after,
&ret_prop) != Success) // 后五个参数是返回值
{
printf("Cannot get %s property.\n", "_NET_WM_PID");
return NULL;
}
else
{
memcpy(&id, ret_prop, 4);// 类型传换
printf("window pid: %d\n", id);
}
}
l 编译$ g++ main.cpp -o main ` pkg-config glib-2.0 --libs --cflags` -lX11
l 执行结果
u 在一个终端打开一个带窗口界面的程序
u 使用以下命令获得窗口的xwindow id
$ xwininfo |grep “Window id”
运行命令后用鼠标点击程序窗口
u 运行我们编写的程序,并将十六进程的xwindow id作为参数传给程序
$ ./main 0x240001e
相关文章推荐
- 例程之九_GUI_获取窗口的进程PID
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
- 根据窗口ID获取进程PID
- 已知进程pid获取其父进程pid
- PowerShell由进程名获取PID
- 获取JAVA当前进程PID的两种方法
- [Android Pro] 获取手机已经安装的应用 和 获取当前正在运行的所有进程(一个uid对应多个pid)
- 根据进程id获取主窗口句柄
- GetProcessMainWnd(获取进程窗口句柄)
- C#获取进程的主窗口句柄的实现方法
- 获取进程pid、根据进程pid获取线程pid、获取线程进程句柄
- 根据PID获取进程名&根据进程名获取PID
- JAVA 获取当前JVM系统进程ID pid
- 由进程获取窗口(CMainframe)句柄
- C#遍历进程获取主窗口句柄
- Linux 精准获取进程pid
- 通过进程名获取进程PID
- C# 获取指定进程的主窗口句柄
- 交互式 Bash Shell 获取进程 pid
- 获取进程pid并添加数组,去重。