自动化RPA开发 --获取所有窗口信息和进程信息

场景

准备做一个RPA工具,可以从桌面和浏览器选择元素,获取窗口信息和进程信息是必要的,因为获取了窗口信息和进程,可用对程序做一些想要的操作。

coding

工具类

java 复制代码
/**
 * Windows系统工具类
 */
public class WinOsUtils {

    static final User32 user32 = User32.INSTANCE;


    /**
     * 获取当前系统下所有的Windows窗口信息
     * @return
     */
    public static List<WindowInfo> allWindows() {

        final List<WindowInfo> windows = new ArrayList<>();


        boolean result = user32.EnumWindows(
                new WinUser.WNDENUMPROC() {
                    public boolean callback(
                            final WinDef.HWND hwnd, final Pointer data) {

                        if (user32.IsWindowVisible(hwnd)) {
                            IntByReference windowPid = new IntByReference();
                            user32.GetWindowThreadProcessId(hwnd, windowPid);

                            String windowTitle = getWindowTitle(hwnd);

                            windows.add(new WindowInfo(hwnd, windowPid.getValue(), windowTitle));
                        }

                        return true;
                    }
                },
                null);

        /* Handle errors. */
        if (!result && Kernel32.INSTANCE.GetLastError() != 0) {
            throw new RuntimeException("Couldn't enumerate windows.");
        }

        /* Return the window list. */
        return windows;
    }


    /**
     * 过窗口句柄来获取指定窗口的标题文本,通常用于窗口管理、自动化测试、或需要识别和操作特定窗口的应用程序中。
     * @param hWnd 句柄
     * @return 窗口标题
     */
    public static String getWindowTitle(WinDef.HWND hWnd) {
        char[] text = new char[1024];
        int length = user32.GetWindowText(hWnd, text, 1024);
        return length > 0 ? new String(text, 0, length) : null;
    }
}

窗口信息

java 复制代码
/**
 * 窗口信息
 */
public class WindowInfo {
    /**
     * 句柄 唯一定义APP的因素
     */
    public WinDef.HWND hwnd;

    /**
     * 进程id
     */
    public int pid;

    /**
     * 标题
     */
    public String title;

    public WindowInfo(WinDef.HWND hwnd, int pid, String title) {
        super();
        this.hwnd = hwnd;
        this.pid = pid;
        this.title = title;
    }

    public WinDef.HWND getHwnd() {
        return hwnd;
    }

    public int getPid() {
        return pid;
    }

    public String getTitle() {
        return title;
    }
}

进程信息

java 复制代码
/**
 * 系统进程信息
 * @Author: Herche Jane
 * @Date: 2023/10/16
 */
public class ProcessInfo {

    /**
     * 进程名称
     */
    private String name;

    /**
     * pid
     */
    private Integer pid;
    
    public ProcessInfo(String name,Integer pid){
        this.name = name;
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

 
    public Integer getPid() {
        return pid;
    }

  
}

结束

这样我们获取了所有的窗口信息和进程信息了,后面有利于我们RPA软件的开发。

相关推荐
KKKlucifer20 分钟前
智能体协同安全服务,提升运维效率与防护精度
运维·安全
小码吃趴菜27 分钟前
服务器预约系统linux小项目-第六节课
运维·服务器
我爱学习好爱好爱33 分钟前
Ansible 自动化部署Elasticsearch + Logstash + Kibana实战(基于RockyLinux 9.6)
elasticsearch·自动化·ansible
爱学习的程序媛34 分钟前
WSL2:Windows上运行Linux的完整指南
linux·运维·windows·ubuntu·wsl2
会飞的大可34 分钟前
Jenkins 企业级集成实战:从规划到落地的完整指南
运维·jenkins
Are_You_Okkk_38 分钟前
AI开源知识库跨部门闭环搭建,效率提升40%
大数据·运维·人工智能·架构·开源
AIminminHu42 分钟前
OpenGL渲染与几何内核那点事-项目实践理论补充(一-2-(3)-当你的协同CAD服务器面临“千人同屏”时:从单机优化到分布式高并发)
运维·服务器·分布式
舰长1151 小时前
Diffie-Hellman Key Agreement Protocol 资源管理错误漏洞(CVE-2022-40735)【原理扫描】openssl升级
运维·服务器
xxjj998a1 小时前
若依部署Nginx和Tomcat
运维·nginx·tomcat
偷心伊普西隆1 小时前
EXCEL 自动化链接更新工具设计方案
自动化·excel