首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用JNA获取活动窗口的应用程序名称?

如何使用JNA获取活动窗口的应用程序名称?
EN

Stack Overflow用户
提问于 2018-06-05 15:44:10
回答 1查看 1.1K关注 0票数 1

下面的代码使用JNA库输出活动窗口的标题。

代码语言:javascript
复制
private static final int MAX_TITLE_LENGTH = 1024;

public static void main(String[] args) throws Exception {

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            char[] buffer = new char[MAX_TITLE_LENGTH * 2];
            HWND hwnd = User32.INSTANCE.GetForegroundWindow();
            User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
            System.out.println("Active window title: " + Native.toString(buffer));
            try {
                BufferedWriter bw = new BufferedWriter(
                    new FileWriter(
                        new File("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\system\\useracivity.txt"),
                        true
                    )
                );
                bw.write(Native.toString(buffer) + " TIME WAS " + dtf.format(now));
                bw.newLine();
                bw.close();
            } catch (Exception e) {
            }
        }
    };

    Timer timer = new Timer();
    timer.schedule(task, new Date(), 5000);

}

但是,我如何获取应用程序的名称,例如Chrome "chrome.exe"?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-17 01:57:55

我发现GetWindowModuleFilename在大多数情况下并不能正常工作。然而,QueryFullProcessImageName运行得很好。请注意,您仍然需要对进程执行OpenProcess操作才能访问图像文件名。

在控制台应用程序中尝试此操作。当您更改窗口时,它将打印出活动窗口的图像文件名。

代码语言:javascript
复制
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;

public class Main {
    public static void main(String[] args) throws Exception {
        HWND prevFg = null;

        while (true) {
            Thread.sleep(200);

            HWND fg = User32.INSTANCE.GetForegroundWindow();

            // don't print the name if it's still the same window as previously
            if (fg.equals(prevFg)) {
                continue;
            }

            String fgImageName = getImageName(fg);
            if (fgImageName == null) {
                System.out.println("Failed to get the image name!");
            } else {
                System.out.println(fgImageName);
            }

            prevFg = fg;
        }
    }

    private static String getImageName(HWND window) {
        // Get the process ID of the window
        IntByReference procId = new IntByReference();
        User32.INSTANCE.GetWindowThreadProcessId(window, procId);

        // Open the process to get permissions to the image name
        HANDLE procHandle = Kernel32.INSTANCE.OpenProcess(
                Kernel32.PROCESS_QUERY_LIMITED_INFORMATION,
                false,
                procId.getValue()
        );

        // Get the image name
        char[] buffer = new char[4096];
        IntByReference bufferSize = new IntByReference(buffer.length);
        boolean success = Kernel32.INSTANCE.QueryFullProcessImageName(procHandle, 0, buffer, bufferSize);

        // Clean up: close the opened process
        Kernel32.INSTANCE.CloseHandle(procHandle);

        return success ? new String(buffer, 0, bufferSize.getValue()) : null;
    }
}

单击我的窗口,程序会打印出如下所示的行:

代码语言:javascript
复制
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.5\bin\idea64.exe
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.5\bin\idea64.exe
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50694754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档