【问题标题】:How can I get the process name from the foreground window on Windows using Java?如何使用 Java 从 Windows 上的前台窗口获取进程名称?
【发布时间】:2016-08-22 15:19:55
【问题描述】:

我正在尝试从前台窗口获取进程的名称,但我遇到了一些问题。

例如,如果我打开 Mozilla Firefox,它会注册类似“www.google.com - Mozilla Firefox”的内容。在 Photoshop 中,它会注册“filename.psd”+您正在处理的缩放或图层名称。

我想从前台窗口获取进程名称,我不希望它检测您正在处理的每一层等,我只想让它获取进程名称(photoshop.exe 或类似的东西)

目前,我正在使用它从该窗口获取前台窗口名称和进程 ID:

public String getActiveProcess(){                   
    char[] buffer;
    HWND hwnd;
    String processName = ".... // ";
    IntByReference IdByRef = new IntByReference();

    //get window information
    buffer = new char[MAX_TITLE_LENGTH * 2];
    hwnd = User32.INSTANCE.GetForegroundWindow();

    //get process ID from window in foreground
    int processid = User32.INSTANCE.GetWindowThreadProcessId(hwnd, IdByRef);

    HANDLE processHandle = Kernel32.INSTANCE.OpenProcess(processid, true, MAX_TITLE_LENGTH);

    return processName;     
}

【问题讨论】:

  • 进程没有名字。为什么不问你想要解决的真正问题,而不是你的解决方案?

标签: java windows process window


【解决方案1】:

假设你可以调用任意Win32函数,你可以使用GetWindowThreadProcessId()获取进程ID,使用OpenProcess()打开一个句柄,使用GetModuleBaseName()获取进程的可执行名称。

【讨论】:

  • 卡在“GetProcessImageFileName()”上。我做了其余的,但我被困在了那里。你能写一些简单的使用例子吗?
  • 改用 GetModuleBaseName()。如果您仍然无法使其正常工作,请将您的新代码添加到问题的末尾,我会看看是否能找出问题所在。
  • 代码已更新。我不知道如何继续。我不知道如何在 java 中使用 GetProcessImageFileName() 或 GetModuleBasename()
猜你喜欢
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多